display_server_x11.cpp 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439
  1. /*************************************************************************/
  2. /* display_server_x11.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "display_server_x11.h"
  31. #ifdef X11_ENABLED
  32. #include "detect_prime_x11.h"
  33. #include "core/os/dir_access.h"
  34. #include "core/print_string.h"
  35. #include "errno.h"
  36. #include "key_mapping_x11.h"
  37. #if defined(OPENGL_ENABLED)
  38. #include "drivers/gles2/rasterizer_gles2.h"
  39. #endif
  40. #if defined(VULKAN_ENABLED)
  41. #include "servers/visual/rasterizer_rd/rasterizer_rd.h"
  42. #endif
  43. #include "scene/resources/texture.h"
  44. #ifdef HAVE_MNTENT
  45. #include <mntent.h>
  46. #endif
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include "X11/Xutil.h"
  51. #include "X11/Xatom.h"
  52. #include "X11/extensions/Xinerama.h"
  53. // ICCCM
  54. #define WM_NormalState 1L // window normal state
  55. #define WM_IconicState 3L // window minimized
  56. // EWMH
  57. #define _NET_WM_STATE_REMOVE 0L // remove/unset property
  58. #define _NET_WM_STATE_ADD 1L // add/set property
  59. #define _NET_WM_STATE_TOGGLE 2L // toggle property
  60. #include "main/main.h"
  61. #include <dlfcn.h>
  62. #include <fcntl.h>
  63. #include <sys/stat.h>
  64. #include <sys/types.h>
  65. #include <unistd.h>
  66. //stupid linux.h
  67. #ifdef KEY_TAB
  68. #undef KEY_TAB
  69. #endif
  70. #include <X11/Xatom.h>
  71. #undef CursorShape
  72. #include <X11/XKBlib.h>
  73. #include "core/project_settings.h"
  74. // 2.2 is the first release with multitouch
  75. #define XINPUT_CLIENT_VERSION_MAJOR 2
  76. #define XINPUT_CLIENT_VERSION_MINOR 2
  77. #define VALUATOR_ABSX 0
  78. #define VALUATOR_ABSY 1
  79. #define VALUATOR_PRESSURE 2
  80. #define VALUATOR_TILTX 3
  81. #define VALUATOR_TILTY 4
  82. static const double abs_resolution_mult = 10000.0;
  83. static const double abs_resolution_range_mult = 10.0;
  84. bool DisplayServerX11::has_feature(Feature p_feature) const {
  85. switch (p_feature) {
  86. case FEATURE_SUBWINDOWS:
  87. #ifdef TOUCH_ENABLED
  88. case FEATURE_TOUCHSCREEN:
  89. #endif
  90. case FEATURE_MOUSE:
  91. case FEATURE_MOUSE_WARP:
  92. case FEATURE_CLIPBOARD:
  93. case FEATURE_CURSOR_SHAPE:
  94. case FEATURE_CUSTOM_CURSOR_SHAPE:
  95. case FEATURE_IME:
  96. case FEATURE_WINDOW_TRANSPARENCY:
  97. //case FEATURE_HIDPI:
  98. case FEATURE_ICON:
  99. case FEATURE_NATIVE_ICON:
  100. case FEATURE_SWAP_BUFFERS:
  101. return true;
  102. default: {
  103. }
  104. }
  105. return false;
  106. }
  107. String DisplayServerX11::get_name() const {
  108. return "X11";
  109. }
  110. void DisplayServerX11::alert(const String &p_alert, const String &p_title) {
  111. const char *message_programs[] = { "zenity", "kdialog", "Xdialog", "xmessage" };
  112. String path = OS::get_singleton()->get_environment("PATH");
  113. Vector<String> path_elems = path.split(":", false);
  114. String program;
  115. for (int i = 0; i < path_elems.size(); i++) {
  116. for (uint64_t k = 0; k < sizeof(message_programs) / sizeof(char *); k++) {
  117. String tested_path = path_elems[i].plus_file(message_programs[k]);
  118. if (FileAccess::exists(tested_path)) {
  119. program = tested_path;
  120. break;
  121. }
  122. }
  123. if (program.length())
  124. break;
  125. }
  126. List<String> args;
  127. if (program.ends_with("zenity")) {
  128. args.push_back("--error");
  129. args.push_back("--width");
  130. args.push_back("500");
  131. args.push_back("--title");
  132. args.push_back(p_title);
  133. args.push_back("--text");
  134. args.push_back(p_alert);
  135. }
  136. if (program.ends_with("kdialog")) {
  137. args.push_back("--error");
  138. args.push_back(p_alert);
  139. args.push_back("--title");
  140. args.push_back(p_title);
  141. }
  142. if (program.ends_with("Xdialog")) {
  143. args.push_back("--title");
  144. args.push_back(p_title);
  145. args.push_back("--msgbox");
  146. args.push_back(p_alert);
  147. args.push_back("0");
  148. args.push_back("0");
  149. }
  150. if (program.ends_with("xmessage")) {
  151. args.push_back("-center");
  152. args.push_back("-title");
  153. args.push_back(p_title);
  154. args.push_back(p_alert);
  155. }
  156. if (program.length()) {
  157. OS::get_singleton()->execute(program, args, true);
  158. } else {
  159. print_line(p_alert);
  160. }
  161. }
  162. void DisplayServerX11::_update_real_mouse_position(const WindowData &wd) {
  163. Window root_return, child_return;
  164. int root_x, root_y, win_x, win_y;
  165. unsigned int mask_return;
  166. Bool xquerypointer_result = XQueryPointer(x11_display, wd.x11_window, &root_return, &child_return, &root_x, &root_y,
  167. &win_x, &win_y, &mask_return);
  168. if (xquerypointer_result) {
  169. if (win_x > 0 && win_y > 0 && win_x <= wd.size.width && win_y <= wd.size.height) {
  170. last_mouse_pos.x = win_x;
  171. last_mouse_pos.y = win_y;
  172. last_mouse_pos_valid = true;
  173. InputFilter::get_singleton()->set_mouse_position(last_mouse_pos);
  174. }
  175. }
  176. }
  177. bool DisplayServerX11::_refresh_device_info() {
  178. int event_base, error_base;
  179. print_verbose("XInput: Refreshing devices.");
  180. if (!XQueryExtension(x11_display, "XInputExtension", &xi.opcode, &event_base, &error_base)) {
  181. print_verbose("XInput extension not available. Please upgrade your distribution.");
  182. return false;
  183. }
  184. int xi_major_query = XINPUT_CLIENT_VERSION_MAJOR;
  185. int xi_minor_query = XINPUT_CLIENT_VERSION_MINOR;
  186. if (XIQueryVersion(x11_display, &xi_major_query, &xi_minor_query) != Success) {
  187. print_verbose(vformat("XInput 2 not available (server supports %d.%d).", xi_major_query, xi_minor_query));
  188. xi.opcode = 0;
  189. return false;
  190. }
  191. if (xi_major_query < XINPUT_CLIENT_VERSION_MAJOR || (xi_major_query == XINPUT_CLIENT_VERSION_MAJOR && xi_minor_query < XINPUT_CLIENT_VERSION_MINOR)) {
  192. print_verbose(vformat("XInput %d.%d not available (server supports %d.%d). Touch input unavailable.",
  193. XINPUT_CLIENT_VERSION_MAJOR, XINPUT_CLIENT_VERSION_MINOR, xi_major_query, xi_minor_query));
  194. }
  195. xi.absolute_devices.clear();
  196. xi.touch_devices.clear();
  197. int dev_count;
  198. XIDeviceInfo *info = XIQueryDevice(x11_display, XIAllDevices, &dev_count);
  199. for (int i = 0; i < dev_count; i++) {
  200. XIDeviceInfo *dev = &info[i];
  201. if (!dev->enabled)
  202. continue;
  203. if (!(dev->use == XIMasterPointer || dev->use == XIFloatingSlave))
  204. continue;
  205. bool direct_touch = false;
  206. bool absolute_mode = false;
  207. int resolution_x = 0;
  208. int resolution_y = 0;
  209. int range_min_x = 0;
  210. int range_min_y = 0;
  211. int range_max_x = 0;
  212. int range_max_y = 0;
  213. int pressure_resolution = 0;
  214. int tilt_resolution_x = 0;
  215. int tilt_resolution_y = 0;
  216. for (int j = 0; j < dev->num_classes; j++) {
  217. #ifdef TOUCH_ENABLED
  218. if (dev->classes[j]->type == XITouchClass && ((XITouchClassInfo *)dev->classes[j])->mode == XIDirectTouch) {
  219. direct_touch = true;
  220. }
  221. #endif
  222. if (dev->classes[j]->type == XIValuatorClass) {
  223. XIValuatorClassInfo *class_info = (XIValuatorClassInfo *)dev->classes[j];
  224. if (class_info->number == VALUATOR_ABSX && class_info->mode == XIModeAbsolute) {
  225. resolution_x = class_info->resolution;
  226. range_min_x = class_info->min;
  227. range_max_x = class_info->max;
  228. absolute_mode = true;
  229. } else if (class_info->number == VALUATOR_ABSY && class_info->mode == XIModeAbsolute) {
  230. resolution_y = class_info->resolution;
  231. range_min_y = class_info->min;
  232. range_max_y = class_info->max;
  233. absolute_mode = true;
  234. } else if (class_info->number == VALUATOR_PRESSURE && class_info->mode == XIModeAbsolute) {
  235. pressure_resolution = (class_info->max - class_info->min);
  236. if (pressure_resolution == 0) pressure_resolution = 1;
  237. } else if (class_info->number == VALUATOR_TILTX && class_info->mode == XIModeAbsolute) {
  238. tilt_resolution_x = (class_info->max - class_info->min);
  239. if (tilt_resolution_x == 0) tilt_resolution_x = 1;
  240. } else if (class_info->number == VALUATOR_TILTY && class_info->mode == XIModeAbsolute) {
  241. tilt_resolution_y = (class_info->max - class_info->min);
  242. if (tilt_resolution_y == 0) tilt_resolution_y = 1;
  243. }
  244. }
  245. }
  246. if (direct_touch) {
  247. xi.touch_devices.push_back(dev->deviceid);
  248. print_verbose("XInput: Using touch device: " + String(dev->name));
  249. }
  250. if (absolute_mode) {
  251. // If no resolution was reported, use the min/max ranges.
  252. if (resolution_x <= 0) {
  253. resolution_x = (range_max_x - range_min_x) * abs_resolution_range_mult;
  254. }
  255. if (resolution_y <= 0) {
  256. resolution_y = (range_max_y - range_min_y) * abs_resolution_range_mult;
  257. }
  258. xi.absolute_devices[dev->deviceid] = Vector2(abs_resolution_mult / resolution_x, abs_resolution_mult / resolution_y);
  259. print_verbose("XInput: Absolute pointing device: " + String(dev->name));
  260. }
  261. xi.pressure = 0;
  262. xi.pen_devices[dev->deviceid] = Vector3(pressure_resolution, tilt_resolution_x, tilt_resolution_y);
  263. }
  264. XIFreeDeviceInfo(info);
  265. #ifdef TOUCH_ENABLED
  266. if (!xi.touch_devices.size()) {
  267. print_verbose("XInput: No touch devices found.");
  268. }
  269. #endif
  270. return true;
  271. }
  272. void DisplayServerX11::_flush_mouse_motion() {
  273. while (true) {
  274. if (XPending(x11_display) > 0) {
  275. XEvent event;
  276. XPeekEvent(x11_display, &event);
  277. if (XGetEventData(x11_display, &event.xcookie) && event.xcookie.type == GenericEvent && event.xcookie.extension == xi.opcode) {
  278. XIDeviceEvent *event_data = (XIDeviceEvent *)event.xcookie.data;
  279. if (event_data->evtype == XI_RawMotion) {
  280. XNextEvent(x11_display, &event);
  281. } else {
  282. break;
  283. }
  284. } else {
  285. break;
  286. }
  287. } else {
  288. break;
  289. }
  290. }
  291. xi.relative_motion.x = 0;
  292. xi.relative_motion.y = 0;
  293. }
  294. void DisplayServerX11::mouse_set_mode(MouseMode p_mode) {
  295. if (p_mode == mouse_mode)
  296. return;
  297. if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED)
  298. XUngrabPointer(x11_display, CurrentTime);
  299. // The only modes that show a cursor are VISIBLE and CONFINED
  300. bool showCursor = (p_mode == MOUSE_MODE_VISIBLE || p_mode == MOUSE_MODE_CONFINED);
  301. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  302. if (showCursor) {
  303. XDefineCursor(x11_display, E->get().x11_window, cursors[current_cursor]); // show cursor
  304. } else {
  305. XDefineCursor(x11_display, E->get().x11_window, null_cursor); // hide cursor
  306. }
  307. }
  308. mouse_mode = p_mode;
  309. if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED) {
  310. //flush pending motion events
  311. _flush_mouse_motion();
  312. WindowData &main_window = windows[MAIN_WINDOW_ID];
  313. if (XGrabPointer(
  314. x11_display, main_window.x11_window, True,
  315. ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
  316. GrabModeAsync, GrabModeAsync, windows[MAIN_WINDOW_ID].x11_window, None, CurrentTime) != GrabSuccess) {
  317. ERR_PRINT("NO GRAB");
  318. }
  319. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  320. center.x = main_window.size.width / 2;
  321. center.y = main_window.size.height / 2;
  322. XWarpPointer(x11_display, None, main_window.x11_window,
  323. 0, 0, 0, 0, (int)center.x, (int)center.y);
  324. InputFilter::get_singleton()->set_mouse_position(center);
  325. }
  326. } else {
  327. do_mouse_warp = false;
  328. }
  329. XFlush(x11_display);
  330. }
  331. DisplayServerX11::MouseMode DisplayServerX11::mouse_get_mode() const {
  332. return mouse_mode;
  333. }
  334. void DisplayServerX11::mouse_warp_to_position(const Point2i &p_to) {
  335. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  336. last_mouse_pos = p_to;
  337. } else {
  338. /*XWindowAttributes xwa;
  339. XGetWindowAttributes(x11_display, x11_window, &xwa);
  340. printf("%d %d\n", xwa.x, xwa.y); needed? */
  341. XWarpPointer(x11_display, None, windows[MAIN_WINDOW_ID].x11_window,
  342. 0, 0, 0, 0, (int)p_to.x, (int)p_to.y);
  343. }
  344. }
  345. Point2i DisplayServerX11::mouse_get_position() const {
  346. return last_mouse_pos;
  347. }
  348. int DisplayServerX11::mouse_get_button_state() const {
  349. return last_button_state;
  350. }
  351. void DisplayServerX11::clipboard_set(const String &p_text) {
  352. internal_clipboard = p_text;
  353. XSetSelectionOwner(x11_display, XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window, CurrentTime);
  354. XSetSelectionOwner(x11_display, XInternAtom(x11_display, "CLIPBOARD", 0), windows[MAIN_WINDOW_ID].x11_window, CurrentTime);
  355. }
  356. static String _clipboard_get_impl(Atom p_source, Window x11_window, ::Display *x11_display, String p_internal_clipboard, Atom target) {
  357. String ret;
  358. Atom type;
  359. Atom selection = XA_PRIMARY;
  360. int format, result;
  361. unsigned long len, bytes_left, dummy;
  362. unsigned char *data;
  363. Window Sown = XGetSelectionOwner(x11_display, p_source);
  364. if (Sown == x11_window) {
  365. return p_internal_clipboard;
  366. };
  367. if (Sown != None) {
  368. XConvertSelection(x11_display, p_source, target, selection,
  369. x11_window, CurrentTime);
  370. XFlush(x11_display);
  371. while (true) {
  372. XEvent event;
  373. XNextEvent(x11_display, &event);
  374. if (event.type == SelectionNotify && event.xselection.requestor == x11_window) {
  375. break;
  376. };
  377. };
  378. //
  379. // Do not get any data, see how much data is there
  380. //
  381. XGetWindowProperty(x11_display, x11_window,
  382. selection, // Tricky..
  383. 0, 0, // offset - len
  384. 0, // Delete 0==FALSE
  385. AnyPropertyType, //flag
  386. &type, // return type
  387. &format, // return format
  388. &len, &bytes_left, //that
  389. &data);
  390. // DATA is There
  391. if (bytes_left > 0) {
  392. result = XGetWindowProperty(x11_display, x11_window,
  393. selection, 0, bytes_left, 0,
  394. AnyPropertyType, &type, &format,
  395. &len, &dummy, &data);
  396. if (result == Success) {
  397. ret.parse_utf8((const char *)data);
  398. } else
  399. printf("FAIL\n");
  400. XFree(data);
  401. }
  402. }
  403. return ret;
  404. }
  405. static String _clipboard_get(Atom p_source, Window x11_window, ::Display *x11_display, String p_internal_clipboard) {
  406. String ret;
  407. Atom utf8_atom = XInternAtom(x11_display, "UTF8_STRING", True);
  408. if (utf8_atom != None) {
  409. ret = _clipboard_get_impl(p_source, x11_window, x11_display, p_internal_clipboard, utf8_atom);
  410. }
  411. if (ret == "") {
  412. ret = _clipboard_get_impl(p_source, x11_window, x11_display, p_internal_clipboard, XA_STRING);
  413. }
  414. return ret;
  415. }
  416. String DisplayServerX11::clipboard_get() const {
  417. String ret;
  418. ret = _clipboard_get(XInternAtom(x11_display, "CLIPBOARD", 0), windows[MAIN_WINDOW_ID].x11_window, x11_display, internal_clipboard);
  419. if (ret == "") {
  420. ret = _clipboard_get(XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window, x11_display, internal_clipboard);
  421. };
  422. return ret;
  423. }
  424. int DisplayServerX11::get_screen_count() const {
  425. // Using Xinerama Extension
  426. int event_base, error_base;
  427. const Bool ext_okay = XineramaQueryExtension(x11_display, &event_base, &error_base);
  428. if (!ext_okay) return 0;
  429. int count;
  430. XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
  431. XFree(xsi);
  432. return count;
  433. }
  434. Point2i DisplayServerX11::screen_get_position(int p_screen) const {
  435. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  436. p_screen = window_get_current_screen();
  437. }
  438. // Using Xinerama Extension
  439. int event_base, error_base;
  440. const Bool ext_okay = XineramaQueryExtension(x11_display, &event_base, &error_base);
  441. if (!ext_okay) {
  442. return Point2i(0, 0);
  443. }
  444. int count;
  445. XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
  446. if (p_screen >= count) {
  447. return Point2i(0, 0);
  448. }
  449. Point2i position = Point2i(xsi[p_screen].x_org, xsi[p_screen].y_org);
  450. XFree(xsi);
  451. return position;
  452. }
  453. Size2i DisplayServerX11::screen_get_size(int p_screen) const {
  454. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  455. p_screen = window_get_current_screen();
  456. }
  457. // Using Xinerama Extension
  458. int event_base, error_base;
  459. const Bool ext_okay = XineramaQueryExtension(x11_display, &event_base, &error_base);
  460. if (!ext_okay) return Size2i(0, 0);
  461. int count;
  462. XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
  463. if (p_screen >= count) return Size2i(0, 0);
  464. Size2i size = Point2i(xsi[p_screen].width, xsi[p_screen].height);
  465. XFree(xsi);
  466. return size;
  467. }
  468. int DisplayServerX11::screen_get_dpi(int p_screen) const {
  469. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  470. p_screen = window_get_current_screen();
  471. }
  472. //invalid screen?
  473. ERR_FAIL_INDEX_V(p_screen, get_screen_count(), 0);
  474. //Get physical monitor Dimensions through XRandR and calculate dpi
  475. Size2i sc = screen_get_size(p_screen);
  476. if (xrandr_ext_ok) {
  477. int count = 0;
  478. if (xrr_get_monitors) {
  479. xrr_monitor_info *monitors = xrr_get_monitors(x11_display, windows[MAIN_WINDOW_ID].x11_window, true, &count);
  480. if (p_screen < count) {
  481. double xdpi = sc.width / (double)monitors[p_screen].mwidth * 25.4;
  482. double ydpi = sc.height / (double)monitors[p_screen].mheight * 25.4;
  483. xrr_free_monitors(monitors);
  484. return (xdpi + ydpi) / 2;
  485. }
  486. xrr_free_monitors(monitors);
  487. } else if (p_screen == 0) {
  488. XRRScreenSize *sizes = XRRSizes(x11_display, 0, &count);
  489. if (sizes) {
  490. double xdpi = sc.width / (double)sizes[0].mwidth * 25.4;
  491. double ydpi = sc.height / (double)sizes[0].mheight * 25.4;
  492. return (xdpi + ydpi) / 2;
  493. }
  494. }
  495. }
  496. int width_mm = DisplayWidthMM(x11_display, p_screen);
  497. int height_mm = DisplayHeightMM(x11_display, p_screen);
  498. double xdpi = (width_mm ? sc.width / (double)width_mm * 25.4 : 0);
  499. double ydpi = (height_mm ? sc.height / (double)height_mm * 25.4 : 0);
  500. if (xdpi || ydpi)
  501. return (xdpi + ydpi) / (xdpi && ydpi ? 2 : 1);
  502. //could not get dpi
  503. return 96;
  504. }
  505. bool DisplayServerX11::screen_is_touchscreen(int p_screen) const {
  506. #ifndef _MSC_VER
  507. #warning Need to get from proper window
  508. #endif
  509. return DisplayServer::screen_is_touchscreen(p_screen);
  510. }
  511. Vector<DisplayServer::WindowID> DisplayServerX11::get_window_list() const {
  512. Vector<int> ret;
  513. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  514. ret.push_back(E->key());
  515. }
  516. return ret;
  517. }
  518. DisplayServer::WindowID DisplayServerX11::create_sub_window(WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect) {
  519. WindowID id = _create_window(p_mode, p_rect.size);
  520. window_set_position(p_rect.position, id);
  521. for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
  522. if (p_flags & (1 << i)) {
  523. window_set_flag(WindowFlags(i), true, id);
  524. }
  525. }
  526. return id;
  527. }
  528. void DisplayServerX11::delete_sub_window(WindowID p_id) {
  529. ERR_FAIL_COND(windows.has(p_id));
  530. ERR_FAIL_COND_MSG(p_id == MAIN_WINDOW_ID, "Main window can't be deleted"); //ma
  531. WindowData &wd = windows[p_id];
  532. #ifdef VULKAN_ENABLED
  533. if (rendering_driver == "vulkan") {
  534. context_vulkan->window_destroy(p_id);
  535. }
  536. #endif
  537. XUnmapWindow(x11_display, wd.x11_window);
  538. XDestroyWindow(x11_display, wd.x11_window);
  539. if (wd.xic) {
  540. XDestroyIC(wd.xic);
  541. }
  542. windows.erase(p_id);
  543. }
  544. void DisplayServerX11::window_set_title(const String &p_title, WindowID p_window) {
  545. ERR_FAIL_COND(!windows.has(p_window));
  546. WindowData &wd = windows[p_window];
  547. XStoreName(x11_display, wd.x11_window, p_title.utf8().get_data());
  548. Atom _net_wm_name = XInternAtom(x11_display, "_NET_WM_NAME", false);
  549. Atom utf8_string = XInternAtom(x11_display, "UTF8_STRING", false);
  550. XChangeProperty(x11_display, wd.x11_window, _net_wm_name, utf8_string, 8, PropModeReplace, (unsigned char *)p_title.utf8().get_data(), p_title.utf8().length());
  551. }
  552. void DisplayServerX11::window_set_resize_callback(const Callable &p_callable, WindowID p_window) {
  553. ERR_FAIL_COND(!windows.has(p_window));
  554. WindowData &wd = windows[p_window];
  555. wd.resize_callback = p_callable;
  556. }
  557. void DisplayServerX11::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  558. ERR_FAIL_COND(!windows.has(p_window));
  559. WindowData &wd = windows[p_window];
  560. wd.event_callback = p_callable;
  561. }
  562. void DisplayServerX11::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  563. ERR_FAIL_COND(!windows.has(p_window));
  564. WindowData &wd = windows[p_window];
  565. wd.input_event_callback = p_callable;
  566. }
  567. void DisplayServerX11::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  568. ERR_FAIL_COND(!windows.has(p_window));
  569. WindowData &wd = windows[p_window];
  570. wd.input_text_callback = p_callable;
  571. }
  572. void DisplayServerX11::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  573. ERR_FAIL_COND(!windows.has(p_window));
  574. WindowData &wd = windows[p_window];
  575. wd.drop_files_callback = p_callable;
  576. }
  577. int DisplayServerX11::window_get_current_screen(WindowID p_window) const {
  578. ERR_FAIL_COND_V(!windows.has(p_window), -1);
  579. const WindowData &wd = windows[p_window];
  580. int x, y;
  581. Window child;
  582. XTranslateCoordinates(x11_display, wd.x11_window, DefaultRootWindow(x11_display), 0, 0, &x, &y, &child);
  583. int count = get_screen_count();
  584. for (int i = 0; i < count; i++) {
  585. Point2i pos = screen_get_position(i);
  586. Size2i size = screen_get_size(i);
  587. if ((x >= pos.x && x < pos.x + size.width) && (y >= pos.y && y < pos.y + size.height))
  588. return i;
  589. }
  590. return 0;
  591. }
  592. void DisplayServerX11::window_set_current_screen(int p_screen, WindowID p_window) {
  593. ERR_FAIL_COND(!windows.has(p_window));
  594. WindowData &wd = windows[p_window];
  595. int count = get_screen_count();
  596. if (p_screen >= count) return;
  597. if (window_get_mode(p_window) == WINDOW_MODE_FULLSCREEN) {
  598. Point2i position = screen_get_position(p_screen);
  599. Size2i size = screen_get_size(p_screen);
  600. XMoveResizeWindow(x11_display, wd.x11_window, position.x, position.y, size.x, size.y);
  601. } else {
  602. if (p_screen != window_get_current_screen(p_window)) {
  603. Point2i position = screen_get_position(p_screen);
  604. XMoveWindow(x11_display, wd.x11_window, position.x, position.y);
  605. }
  606. }
  607. }
  608. Point2i DisplayServerX11::window_get_position(WindowID p_window) const {
  609. ERR_FAIL_COND_V(!windows.has(p_window), Point2i());
  610. const WindowData &wd = windows[p_window];
  611. int x, y;
  612. Window child;
  613. XTranslateCoordinates(x11_display, wd.x11_window, DefaultRootWindow(x11_display), 0, 0, &x, &y, &child);
  614. return Point2i(x, y);
  615. }
  616. void DisplayServerX11::window_set_position(const Point2i &p_position, WindowID p_window) {
  617. ERR_FAIL_COND(!windows.has(p_window));
  618. WindowData &wd = windows[p_window];
  619. int x = 0;
  620. int y = 0;
  621. if (!window_get_flag(WINDOW_FLAG_BORDERLESS, p_window)) {
  622. //exclude window decorations
  623. XSync(x11_display, False);
  624. Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True);
  625. if (prop != None) {
  626. Atom type;
  627. int format;
  628. unsigned long len;
  629. unsigned long remaining;
  630. unsigned char *data = NULL;
  631. if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) {
  632. if (format == 32 && len == 4) {
  633. long *extents = (long *)data;
  634. x = extents[0];
  635. y = extents[2];
  636. }
  637. XFree(data);
  638. }
  639. }
  640. }
  641. XMoveWindow(x11_display, wd.x11_window, p_position.x - x, p_position.y - y);
  642. _update_real_mouse_position(wd);
  643. }
  644. void DisplayServerX11::window_set_max_size(const Size2i p_size, WindowID p_window) {
  645. ERR_FAIL_COND(!windows.has(p_window));
  646. WindowData &wd = windows[p_window];
  647. if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) {
  648. ERR_PRINT("Maximum window size can't be smaller than minimum window size!");
  649. return;
  650. }
  651. wd.max_size = p_size;
  652. if (!window_get_flag(WINDOW_FLAG_RESIZE_DISABLED, p_window)) {
  653. XSizeHints *xsh;
  654. xsh = XAllocSizeHints();
  655. xsh->flags = 0L;
  656. if (wd.min_size != Size2i()) {
  657. xsh->flags |= PMinSize;
  658. xsh->min_width = wd.min_size.x;
  659. xsh->min_height = wd.min_size.y;
  660. }
  661. if (wd.max_size != Size2i()) {
  662. xsh->flags |= PMaxSize;
  663. xsh->max_width = wd.max_size.x;
  664. xsh->max_height = wd.max_size.y;
  665. }
  666. XSetWMNormalHints(x11_display, wd.x11_window, xsh);
  667. XFree(xsh);
  668. XFlush(x11_display);
  669. }
  670. }
  671. Size2i DisplayServerX11::window_get_max_size(WindowID p_window) const {
  672. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  673. const WindowData &wd = windows[p_window];
  674. return wd.max_size;
  675. }
  676. void DisplayServerX11::window_set_min_size(const Size2i p_size, WindowID p_window) {
  677. ERR_FAIL_COND(!windows.has(p_window));
  678. WindowData &wd = windows[p_window];
  679. if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) {
  680. ERR_PRINT("Minimum window size can't be larger than maximum window size!");
  681. return;
  682. }
  683. wd.min_size = p_size;
  684. if (!window_get_flag(WINDOW_FLAG_RESIZE_DISABLED, p_window)) {
  685. XSizeHints *xsh;
  686. xsh = XAllocSizeHints();
  687. xsh->flags = 0L;
  688. if (wd.min_size != Size2i()) {
  689. xsh->flags |= PMinSize;
  690. xsh->min_width = wd.min_size.x;
  691. xsh->min_height = wd.min_size.y;
  692. }
  693. if (wd.max_size != Size2i()) {
  694. xsh->flags |= PMaxSize;
  695. xsh->max_width = wd.max_size.x;
  696. xsh->max_height = wd.max_size.y;
  697. }
  698. XSetWMNormalHints(x11_display, wd.x11_window, xsh);
  699. XFree(xsh);
  700. XFlush(x11_display);
  701. }
  702. }
  703. Size2i DisplayServerX11::window_get_min_size(WindowID p_window) const {
  704. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  705. const WindowData &wd = windows[p_window];
  706. return wd.min_size;
  707. }
  708. void DisplayServerX11::window_set_size(const Size2i p_size, WindowID p_window) {
  709. ERR_FAIL_COND(!windows.has(p_window));
  710. WindowData &wd = windows[p_window];
  711. if (wd.size.width == p_size.width && wd.size.height == p_size.height)
  712. return;
  713. XWindowAttributes xwa;
  714. XSync(x11_display, False);
  715. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  716. int old_w = xwa.width;
  717. int old_h = xwa.height;
  718. // If window resizable is disabled we need to update the attributes first
  719. XSizeHints *xsh;
  720. xsh = XAllocSizeHints();
  721. if (!window_get_flag(WINDOW_FLAG_RESIZE_DISABLED, p_window)) {
  722. xsh->flags = PMinSize | PMaxSize;
  723. xsh->min_width = p_size.x;
  724. xsh->max_width = p_size.x;
  725. xsh->min_height = p_size.y;
  726. xsh->max_height = p_size.y;
  727. } else {
  728. xsh->flags = 0L;
  729. if (wd.min_size != Size2i()) {
  730. xsh->flags |= PMinSize;
  731. xsh->min_width = wd.min_size.x;
  732. xsh->min_height = wd.min_size.y;
  733. }
  734. if (wd.max_size != Size2i()) {
  735. xsh->flags |= PMaxSize;
  736. xsh->max_width = wd.max_size.x;
  737. xsh->max_height = wd.max_size.y;
  738. }
  739. }
  740. XSetWMNormalHints(x11_display, wd.x11_window, xsh);
  741. XFree(xsh);
  742. // Resize the window
  743. XResizeWindow(x11_display, wd.x11_window, p_size.x, p_size.y);
  744. // Update our videomode width and height
  745. wd.size = p_size;
  746. for (int timeout = 0; timeout < 50; ++timeout) {
  747. XSync(x11_display, False);
  748. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  749. if (old_w != xwa.width || old_h != xwa.height)
  750. break;
  751. usleep(10000);
  752. }
  753. }
  754. Size2i DisplayServerX11::window_get_size(WindowID p_window) const {
  755. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  756. const WindowData &wd = windows[p_window];
  757. return wd.size;
  758. }
  759. Size2i DisplayServerX11::window_get_real_size(WindowID p_window) const {
  760. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  761. const WindowData &wd = windows[p_window];
  762. XWindowAttributes xwa;
  763. XSync(x11_display, False);
  764. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  765. int w = xwa.width;
  766. int h = xwa.height;
  767. Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True);
  768. if (prop != None) {
  769. Atom type;
  770. int format;
  771. unsigned long len;
  772. unsigned long remaining;
  773. unsigned char *data = NULL;
  774. if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) {
  775. if (format == 32 && len == 4) {
  776. long *extents = (long *)data;
  777. w += extents[0] + extents[1]; // left, right
  778. h += extents[2] + extents[3]; // top, bottom
  779. }
  780. XFree(data);
  781. }
  782. }
  783. return Size2(w, h);
  784. }
  785. bool DisplayServerX11::window_is_maximize_allowed(WindowID p_window) const {
  786. ERR_FAIL_COND_V(!windows.has(p_window), false);
  787. const WindowData &wd = windows[p_window];
  788. Atom property = XInternAtom(x11_display, "_NET_WM_ALLOWED_ACTIONS", False);
  789. Atom type;
  790. int format;
  791. unsigned long len;
  792. unsigned long remaining;
  793. unsigned char *data = NULL;
  794. int result = XGetWindowProperty(
  795. x11_display,
  796. wd.x11_window,
  797. property,
  798. 0,
  799. 1024,
  800. False,
  801. XA_ATOM,
  802. &type,
  803. &format,
  804. &len,
  805. &remaining,
  806. &data);
  807. if (result == Success) {
  808. Atom *atoms = (Atom *)data;
  809. Atom wm_act_max_horz = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_HORZ", False);
  810. Atom wm_act_max_vert = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_VERT", False);
  811. bool found_wm_act_max_horz = false;
  812. bool found_wm_act_max_vert = false;
  813. for (uint64_t i = 0; i < len; i++) {
  814. if (atoms[i] == wm_act_max_horz)
  815. found_wm_act_max_horz = true;
  816. if (atoms[i] == wm_act_max_vert)
  817. found_wm_act_max_vert = true;
  818. if (found_wm_act_max_horz || found_wm_act_max_vert)
  819. return true;
  820. }
  821. XFree(atoms);
  822. }
  823. return false;
  824. }
  825. void DisplayServerX11::_set_wm_maximized(WindowID p_window, bool p_enabled) {
  826. ERR_FAIL_COND(!windows.has(p_window));
  827. WindowData &wd = windows[p_window];
  828. // Using EWMH -- Extended Window Manager Hints
  829. XEvent xev;
  830. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  831. Atom wm_max_horz = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  832. Atom wm_max_vert = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  833. memset(&xev, 0, sizeof(xev));
  834. xev.type = ClientMessage;
  835. xev.xclient.window = wd.x11_window;
  836. xev.xclient.message_type = wm_state;
  837. xev.xclient.format = 32;
  838. xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  839. xev.xclient.data.l[1] = wm_max_horz;
  840. xev.xclient.data.l[2] = wm_max_vert;
  841. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  842. if (p_enabled && window_is_maximize_allowed(p_window)) {
  843. // Wait for effective resizing (so the GLX context is too).
  844. // Give up after 0.5s, it's not going to happen on this WM.
  845. // https://github.com/godotengine/godot/issues/19978
  846. for (int attempt = 0; window_get_mode(p_window) != WINDOW_MODE_MAXIMIZED && attempt < 50; attempt++) {
  847. usleep(10000);
  848. }
  849. }
  850. }
  851. void DisplayServerX11::_set_wm_fullscreen(WindowID p_window, bool p_enabled) {
  852. ERR_FAIL_COND(!windows.has(p_window));
  853. WindowData &wd = windows[p_window];
  854. if (p_enabled && !window_get_flag(WINDOW_FLAG_BORDERLESS, p_window)) {
  855. // remove decorations if the window is not already borderless
  856. Hints hints;
  857. Atom property;
  858. hints.flags = 2;
  859. hints.decorations = 0;
  860. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  861. XChangeProperty(x11_display, wd.x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  862. }
  863. if (p_enabled && window_get_flag(WINDOW_FLAG_RESIZE_DISABLED, p_window)) {
  864. // Set the window as resizable to prevent window managers to ignore the fullscreen state flag.
  865. XSizeHints *xsh;
  866. xsh = XAllocSizeHints();
  867. xsh->flags = 0L;
  868. XSetWMNormalHints(x11_display, wd.x11_window, xsh);
  869. XFree(xsh);
  870. }
  871. // Using EWMH -- Extended Window Manager Hints
  872. XEvent xev;
  873. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  874. Atom wm_fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False);
  875. memset(&xev, 0, sizeof(xev));
  876. xev.type = ClientMessage;
  877. xev.xclient.window = wd.x11_window;
  878. xev.xclient.message_type = wm_state;
  879. xev.xclient.format = 32;
  880. xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  881. xev.xclient.data.l[1] = wm_fullscreen;
  882. xev.xclient.data.l[2] = 0;
  883. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  884. // set bypass compositor hint
  885. Atom bypass_compositor = XInternAtom(x11_display, "_NET_WM_BYPASS_COMPOSITOR", False);
  886. unsigned long compositing_disable_on = p_enabled ? 1 : 0;
  887. XChangeProperty(x11_display, wd.x11_window, bypass_compositor, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&compositing_disable_on, 1);
  888. XFlush(x11_display);
  889. if (!p_enabled) {
  890. // Reset the non-resizable flags if we un-set these before.
  891. Size2 size = window_get_size(p_window);
  892. XSizeHints *xsh;
  893. xsh = XAllocSizeHints();
  894. if (window_get_flag(WINDOW_FLAG_RESIZE_DISABLED, p_window)) {
  895. xsh->flags = PMinSize | PMaxSize;
  896. xsh->min_width = size.x;
  897. xsh->max_width = size.x;
  898. xsh->min_height = size.y;
  899. xsh->max_height = size.y;
  900. } else {
  901. xsh->flags = 0L;
  902. if (wd.min_size != Size2()) {
  903. xsh->flags |= PMinSize;
  904. xsh->min_width = wd.min_size.x;
  905. xsh->min_height = wd.min_size.y;
  906. }
  907. if (wd.max_size != Size2()) {
  908. xsh->flags |= PMaxSize;
  909. xsh->max_width = wd.max_size.x;
  910. xsh->max_height = wd.max_size.y;
  911. }
  912. }
  913. XSetWMNormalHints(x11_display, wd.x11_window, xsh);
  914. XFree(xsh);
  915. // put back or remove decorations according to the last set borderless state
  916. Hints hints;
  917. Atom property;
  918. hints.flags = 2;
  919. hints.decorations = window_get_flag(WINDOW_FLAG_BORDERLESS, p_window) ? 0 : 1;
  920. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  921. XChangeProperty(x11_display, wd.x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  922. }
  923. }
  924. void DisplayServerX11::window_set_mode(WindowMode p_mode, WindowID p_window) {
  925. ERR_FAIL_COND(!windows.has(p_window));
  926. WindowData &wd = windows[p_window];
  927. WindowMode old_mode = window_get_mode(p_window);
  928. if (old_mode == p_mode) {
  929. return; // do nothing
  930. }
  931. //remove all "extra" modes
  932. switch (old_mode) {
  933. case WINDOW_MODE_WINDOWED: {
  934. //do nothing
  935. } break;
  936. case WINDOW_MODE_MINIMIZED: {
  937. //Un-Minimize
  938. // Using ICCCM -- Inter-Client Communication Conventions Manual
  939. XEvent xev;
  940. Atom wm_change = XInternAtom(x11_display, "WM_CHANGE_STATE", False);
  941. memset(&xev, 0, sizeof(xev));
  942. xev.type = ClientMessage;
  943. xev.xclient.window = wd.x11_window;
  944. xev.xclient.message_type = wm_change;
  945. xev.xclient.format = 32;
  946. xev.xclient.data.l[0] = WM_NormalState;
  947. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  948. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  949. Atom wm_hidden = XInternAtom(x11_display, "_NET_WM_STATE_HIDDEN", False);
  950. memset(&xev, 0, sizeof(xev));
  951. xev.type = ClientMessage;
  952. xev.xclient.window = wd.x11_window;
  953. xev.xclient.message_type = wm_state;
  954. xev.xclient.format = 32;
  955. xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
  956. xev.xclient.data.l[1] = wm_hidden;
  957. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  958. } break;
  959. case WINDOW_MODE_FULLSCREEN: {
  960. //Remove full-screen
  961. _set_wm_fullscreen(p_window, false);
  962. //un-maximize required for always on top
  963. bool on_top = window_get_flag(WINDOW_FLAG_ALWAYS_ON_TOP, p_window);
  964. wd.fullscreen = false;
  965. window_set_position(wd.last_position_before_fs, p_window);
  966. if (on_top) {
  967. _set_wm_maximized(p_window, false);
  968. }
  969. } break;
  970. case WINDOW_MODE_MAXIMIZED: {
  971. _set_wm_maximized(p_window, false);
  972. } break;
  973. }
  974. switch (p_mode) {
  975. case WINDOW_MODE_WINDOWED: {
  976. //do nothing
  977. } break;
  978. case WINDOW_MODE_MINIMIZED: {
  979. // Using ICCCM -- Inter-Client Communication Conventions Manual
  980. XEvent xev;
  981. Atom wm_change = XInternAtom(x11_display, "WM_CHANGE_STATE", False);
  982. memset(&xev, 0, sizeof(xev));
  983. xev.type = ClientMessage;
  984. xev.xclient.window = wd.x11_window;
  985. xev.xclient.message_type = wm_change;
  986. xev.xclient.format = 32;
  987. xev.xclient.data.l[0] = WM_IconicState;
  988. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  989. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  990. Atom wm_hidden = XInternAtom(x11_display, "_NET_WM_STATE_HIDDEN", False);
  991. memset(&xev, 0, sizeof(xev));
  992. xev.type = ClientMessage;
  993. xev.xclient.window = wd.x11_window;
  994. xev.xclient.message_type = wm_state;
  995. xev.xclient.format = 32;
  996. xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
  997. xev.xclient.data.l[1] = wm_hidden;
  998. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  999. } break;
  1000. case WINDOW_MODE_FULLSCREEN: {
  1001. wd.last_position_before_fs = window_get_position(p_window);
  1002. if (window_get_flag(WINDOW_FLAG_ALWAYS_ON_TOP, p_window)) {
  1003. _set_wm_maximized(p_window, true);
  1004. }
  1005. _set_wm_fullscreen(p_window, true);
  1006. wd.fullscreen = true;
  1007. } break;
  1008. case WINDOW_MODE_MAXIMIZED: {
  1009. _set_wm_maximized(p_window, true);
  1010. } break;
  1011. }
  1012. }
  1013. DisplayServer::WindowMode DisplayServerX11::window_get_mode(WindowID p_window) const {
  1014. ERR_FAIL_COND_V(!windows.has(p_window), WINDOW_MODE_WINDOWED);
  1015. const WindowData &wd = windows[p_window];
  1016. if (wd.fullscreen) { //if fullscreen, it's not in another mode
  1017. return WINDOW_MODE_FULLSCREEN;
  1018. }
  1019. { //test maximized
  1020. // Using EWMH -- Extended Window Manager Hints
  1021. Atom property = XInternAtom(x11_display, "_NET_WM_STATE", False);
  1022. Atom type;
  1023. int format;
  1024. unsigned long len;
  1025. unsigned long remaining;
  1026. unsigned char *data = NULL;
  1027. bool retval = false;
  1028. int result = XGetWindowProperty(
  1029. x11_display,
  1030. wd.x11_window,
  1031. property,
  1032. 0,
  1033. 1024,
  1034. False,
  1035. XA_ATOM,
  1036. &type,
  1037. &format,
  1038. &len,
  1039. &remaining,
  1040. &data);
  1041. if (result == Success) {
  1042. Atom *atoms = (Atom *)data;
  1043. Atom wm_max_horz = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  1044. Atom wm_max_vert = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  1045. bool found_wm_max_horz = false;
  1046. bool found_wm_max_vert = false;
  1047. for (uint64_t i = 0; i < len; i++) {
  1048. if (atoms[i] == wm_max_horz)
  1049. found_wm_max_horz = true;
  1050. if (atoms[i] == wm_max_vert)
  1051. found_wm_max_vert = true;
  1052. if (found_wm_max_horz && found_wm_max_vert) {
  1053. retval = true;
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. XFree(data);
  1059. if (retval) {
  1060. return WINDOW_MODE_MAXIMIZED;
  1061. }
  1062. }
  1063. { // test minimzed
  1064. // Using ICCCM -- Inter-Client Communication Conventions Manual
  1065. Atom property = XInternAtom(x11_display, "WM_STATE", True);
  1066. Atom type;
  1067. int format;
  1068. unsigned long len;
  1069. unsigned long remaining;
  1070. unsigned char *data = NULL;
  1071. int result = XGetWindowProperty(
  1072. x11_display,
  1073. wd.x11_window,
  1074. property,
  1075. 0,
  1076. 32,
  1077. False,
  1078. AnyPropertyType,
  1079. &type,
  1080. &format,
  1081. &len,
  1082. &remaining,
  1083. &data);
  1084. if (result == Success) {
  1085. long *state = (long *)data;
  1086. if (state[0] == WM_IconicState)
  1087. return WINDOW_MODE_MINIMIZED;
  1088. }
  1089. }
  1090. // all other discarded, return windowed.
  1091. return WINDOW_MODE_WINDOWED;
  1092. }
  1093. void DisplayServerX11::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  1094. ERR_FAIL_COND(!windows.has(p_window));
  1095. WindowData &wd = windows[p_window];
  1096. switch (p_flag) {
  1097. case WINDOW_FLAG_RESIZE_DISABLED: {
  1098. XSizeHints *xsh;
  1099. xsh = XAllocSizeHints();
  1100. if (p_enabled) {
  1101. Size2 size = window_get_size(p_window);
  1102. xsh->flags = PMinSize | PMaxSize;
  1103. xsh->min_width = size.x;
  1104. xsh->max_width = size.x;
  1105. xsh->min_height = size.y;
  1106. xsh->max_height = size.y;
  1107. } else {
  1108. xsh->flags = 0L;
  1109. if (wd.min_size != Size2()) {
  1110. xsh->flags |= PMinSize;
  1111. xsh->min_width = wd.min_size.x;
  1112. xsh->min_height = wd.min_size.y;
  1113. }
  1114. if (wd.max_size != Size2()) {
  1115. xsh->flags |= PMaxSize;
  1116. xsh->max_width = wd.max_size.x;
  1117. xsh->max_height = wd.max_size.y;
  1118. }
  1119. }
  1120. XSetWMNormalHints(x11_display, wd.x11_window, xsh);
  1121. XFree(xsh);
  1122. wd.resize_disabled = p_enabled;
  1123. XFlush(x11_display);
  1124. } break;
  1125. case WINDOW_FLAG_BORDERLESS: {
  1126. Hints hints;
  1127. Atom property;
  1128. hints.flags = 2;
  1129. hints.decorations = p_enabled ? 0 : 1;
  1130. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  1131. XChangeProperty(x11_display, wd.x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  1132. // Preserve window size
  1133. window_set_size(window_get_size(p_window), p_window);
  1134. wd.borderless = p_enabled;
  1135. } break;
  1136. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  1137. if (p_enabled && wd.fullscreen) {
  1138. _set_wm_maximized(p_window, true);
  1139. }
  1140. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  1141. Atom wm_above = XInternAtom(x11_display, "_NET_WM_STATE_ABOVE", False);
  1142. XClientMessageEvent xev;
  1143. memset(&xev, 0, sizeof(xev));
  1144. xev.type = ClientMessage;
  1145. xev.window = wd.x11_window;
  1146. xev.message_type = wm_state;
  1147. xev.format = 32;
  1148. xev.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  1149. xev.data.l[1] = wm_above;
  1150. xev.data.l[3] = 1;
  1151. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xev);
  1152. if (!p_enabled && !wd.fullscreen) {
  1153. _set_wm_maximized(p_window, false);
  1154. }
  1155. wd.on_top = p_enabled;
  1156. } break;
  1157. case WINDOW_FLAG_TRANSPARENT: {
  1158. //todo reimplement
  1159. } break;
  1160. default: {
  1161. }
  1162. }
  1163. }
  1164. bool DisplayServerX11::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  1165. ERR_FAIL_COND_V(!windows.has(p_window), false);
  1166. const WindowData &wd = windows[p_window];
  1167. switch (p_flag) {
  1168. case WINDOW_FLAG_RESIZE_DISABLED: {
  1169. return wd.resize_disabled;
  1170. } break;
  1171. case WINDOW_FLAG_BORDERLESS: {
  1172. bool borderless = wd.borderless;
  1173. Atom prop = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  1174. if (prop != None) {
  1175. Atom type;
  1176. int format;
  1177. unsigned long len;
  1178. unsigned long remaining;
  1179. unsigned char *data = NULL;
  1180. if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, sizeof(Hints), False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) {
  1181. if (data && (format == 32) && (len >= 5)) {
  1182. borderless = !((Hints *)data)->decorations;
  1183. }
  1184. XFree(data);
  1185. }
  1186. }
  1187. return borderless;
  1188. } break;
  1189. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  1190. return wd.on_top;
  1191. } break;
  1192. case WINDOW_FLAG_TRANSPARENT: {
  1193. //todo reimplement
  1194. } break;
  1195. default: {
  1196. }
  1197. }
  1198. return false;
  1199. }
  1200. void DisplayServerX11::window_request_attention(WindowID p_window) {
  1201. ERR_FAIL_COND(!windows.has(p_window));
  1202. WindowData &wd = windows[p_window];
  1203. // Using EWMH -- Extended Window Manager Hints
  1204. //
  1205. // Sets the _NET_WM_STATE_DEMANDS_ATTENTION atom for WM_STATE
  1206. // Will be unset by the window manager after user react on the request for attention
  1207. XEvent xev;
  1208. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  1209. Atom wm_attention = XInternAtom(x11_display, "_NET_WM_STATE_DEMANDS_ATTENTION", False);
  1210. memset(&xev, 0, sizeof(xev));
  1211. xev.type = ClientMessage;
  1212. xev.xclient.window = wd.x11_window;
  1213. xev.xclient.message_type = wm_state;
  1214. xev.xclient.format = 32;
  1215. xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
  1216. xev.xclient.data.l[1] = wm_attention;
  1217. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  1218. XFlush(x11_display);
  1219. }
  1220. void DisplayServerX11::window_move_to_foreground(WindowID p_window) {
  1221. ERR_FAIL_COND(!windows.has(p_window));
  1222. WindowData &wd = windows[p_window];
  1223. XEvent xev;
  1224. Atom net_active_window = XInternAtom(x11_display, "_NET_ACTIVE_WINDOW", False);
  1225. memset(&xev, 0, sizeof(xev));
  1226. xev.type = ClientMessage;
  1227. xev.xclient.window = wd.x11_window;
  1228. xev.xclient.message_type = net_active_window;
  1229. xev.xclient.format = 32;
  1230. xev.xclient.data.l[0] = 1;
  1231. xev.xclient.data.l[1] = CurrentTime;
  1232. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  1233. XFlush(x11_display);
  1234. }
  1235. bool DisplayServerX11::window_can_draw(WindowID p_window) const {
  1236. //this seems to be all that is provided by X11
  1237. return window_get_mode(p_window) != WINDOW_MODE_MINIMIZED;
  1238. }
  1239. bool DisplayServerX11::can_any_window_draw() const {
  1240. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1241. if (window_get_mode(E->key()) != WINDOW_MODE_MINIMIZED) {
  1242. return true;
  1243. }
  1244. }
  1245. return false;
  1246. }
  1247. void DisplayServerX11::window_set_ime_active(const bool p_active, WindowID p_window) {
  1248. ERR_FAIL_COND(!windows.has(p_window));
  1249. WindowData &wd = windows[p_window];
  1250. wd.im_active = p_active;
  1251. if (!wd.xic)
  1252. return;
  1253. if (p_active) {
  1254. XSetICFocus(wd.xic);
  1255. window_set_ime_position(wd.im_position, p_window);
  1256. } else {
  1257. XUnsetICFocus(wd.xic);
  1258. }
  1259. }
  1260. void DisplayServerX11::window_set_ime_position(const Point2i &p_pos, WindowID p_window) {
  1261. ERR_FAIL_COND(!windows.has(p_window));
  1262. WindowData &wd = windows[p_window];
  1263. wd.im_position = p_pos;
  1264. if (!wd.xic)
  1265. return;
  1266. ::XPoint spot;
  1267. spot.x = short(p_pos.x);
  1268. spot.y = short(p_pos.y);
  1269. XVaNestedList preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL);
  1270. XSetICValues(wd.xic, XNPreeditAttributes, preedit_attr, NULL);
  1271. XFree(preedit_attr);
  1272. }
  1273. void DisplayServerX11::cursor_set_shape(CursorShape p_shape) {
  1274. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  1275. if (p_shape == current_cursor) {
  1276. return;
  1277. }
  1278. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  1279. if (cursors[p_shape] != None) {
  1280. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1281. XDefineCursor(x11_display, E->get().x11_window, cursors[p_shape]);
  1282. }
  1283. } else if (cursors[CURSOR_ARROW] != None) {
  1284. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1285. XDefineCursor(x11_display, E->get().x11_window, cursors[CURSOR_ARROW]);
  1286. }
  1287. }
  1288. }
  1289. current_cursor = p_shape;
  1290. }
  1291. DisplayServerX11::CursorShape DisplayServerX11::cursor_get_shape() const {
  1292. return current_cursor;
  1293. }
  1294. void DisplayServerX11::cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  1295. if (p_cursor.is_valid()) {
  1296. Map<CursorShape, Vector<Variant>>::Element *cursor_c = cursors_cache.find(p_shape);
  1297. if (cursor_c) {
  1298. if (cursor_c->get()[0] == p_cursor && cursor_c->get()[1] == p_hotspot) {
  1299. cursor_set_shape(p_shape);
  1300. return;
  1301. }
  1302. cursors_cache.erase(p_shape);
  1303. }
  1304. Ref<Texture2D> texture = p_cursor;
  1305. Ref<AtlasTexture> atlas_texture = p_cursor;
  1306. Ref<Image> image;
  1307. Size2 texture_size;
  1308. Rect2 atlas_rect;
  1309. if (texture.is_valid()) {
  1310. image = texture->get_data();
  1311. }
  1312. if (!image.is_valid() && atlas_texture.is_valid()) {
  1313. texture = atlas_texture->get_atlas();
  1314. atlas_rect.size.width = texture->get_width();
  1315. atlas_rect.size.height = texture->get_height();
  1316. atlas_rect.position.x = atlas_texture->get_region().position.x;
  1317. atlas_rect.position.y = atlas_texture->get_region().position.y;
  1318. texture_size.width = atlas_texture->get_region().size.x;
  1319. texture_size.height = atlas_texture->get_region().size.y;
  1320. } else if (image.is_valid()) {
  1321. texture_size.width = texture->get_width();
  1322. texture_size.height = texture->get_height();
  1323. }
  1324. ERR_FAIL_COND(!texture.is_valid());
  1325. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  1326. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  1327. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  1328. image = texture->get_data();
  1329. ERR_FAIL_COND(!image.is_valid());
  1330. // Create the cursor structure
  1331. XcursorImage *cursor_image = XcursorImageCreate(texture_size.width, texture_size.height);
  1332. XcursorUInt image_size = texture_size.width * texture_size.height;
  1333. XcursorDim size = sizeof(XcursorPixel) * image_size;
  1334. cursor_image->version = 1;
  1335. cursor_image->size = size;
  1336. cursor_image->xhot = p_hotspot.x;
  1337. cursor_image->yhot = p_hotspot.y;
  1338. // allocate memory to contain the whole file
  1339. cursor_image->pixels = (XcursorPixel *)memalloc(size);
  1340. for (XcursorPixel index = 0; index < image_size; index++) {
  1341. int row_index = floor(index / texture_size.width) + atlas_rect.position.y;
  1342. int column_index = (index % int(texture_size.width)) + atlas_rect.position.x;
  1343. if (atlas_texture.is_valid()) {
  1344. column_index = MIN(column_index, atlas_rect.size.width - 1);
  1345. row_index = MIN(row_index, atlas_rect.size.height - 1);
  1346. }
  1347. *(cursor_image->pixels + index) = image->get_pixel(column_index, row_index).to_argb32();
  1348. }
  1349. ERR_FAIL_COND(cursor_image->pixels == NULL);
  1350. // Save it for a further usage
  1351. cursors[p_shape] = XcursorImageLoadCursor(x11_display, cursor_image);
  1352. Vector<Variant> params;
  1353. params.push_back(p_cursor);
  1354. params.push_back(p_hotspot);
  1355. cursors_cache.insert(p_shape, params);
  1356. if (p_shape == current_cursor) {
  1357. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  1358. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1359. XDefineCursor(x11_display, E->get().x11_window, cursors[p_shape]);
  1360. }
  1361. }
  1362. }
  1363. memfree(cursor_image->pixels);
  1364. XcursorImageDestroy(cursor_image);
  1365. } else {
  1366. // Reset to default system cursor
  1367. if (img[p_shape]) {
  1368. cursors[p_shape] = XcursorImageLoadCursor(x11_display, img[p_shape]);
  1369. }
  1370. CursorShape c = current_cursor;
  1371. current_cursor = CURSOR_MAX;
  1372. cursor_set_shape(c);
  1373. cursors_cache.erase(p_shape);
  1374. }
  1375. }
  1376. DisplayServerX11::LatinKeyboardVariant DisplayServerX11::get_latin_keyboard_variant() const {
  1377. XkbDescRec *xkbdesc = XkbAllocKeyboard();
  1378. ERR_FAIL_COND_V(!xkbdesc, LATIN_KEYBOARD_QWERTY);
  1379. XkbGetNames(x11_display, XkbSymbolsNameMask, xkbdesc);
  1380. ERR_FAIL_COND_V(!xkbdesc->names, LATIN_KEYBOARD_QWERTY);
  1381. ERR_FAIL_COND_V(!xkbdesc->names->symbols, LATIN_KEYBOARD_QWERTY);
  1382. char *layout = XGetAtomName(x11_display, xkbdesc->names->symbols);
  1383. ERR_FAIL_COND_V(!layout, LATIN_KEYBOARD_QWERTY);
  1384. Vector<String> info = String(layout).split("+");
  1385. ERR_FAIL_INDEX_V(1, info.size(), LATIN_KEYBOARD_QWERTY);
  1386. if (info[1].find("colemak") != -1) {
  1387. return LATIN_KEYBOARD_COLEMAK;
  1388. } else if (info[1].find("qwertz") != -1) {
  1389. return LATIN_KEYBOARD_QWERTZ;
  1390. } else if (info[1].find("azerty") != -1) {
  1391. return LATIN_KEYBOARD_AZERTY;
  1392. } else if (info[1].find("qzerty") != -1) {
  1393. return LATIN_KEYBOARD_QZERTY;
  1394. } else if (info[1].find("dvorak") != -1) {
  1395. return LATIN_KEYBOARD_DVORAK;
  1396. } else if (info[1].find("neo") != -1) {
  1397. return LATIN_KEYBOARD_NEO;
  1398. }
  1399. return LATIN_KEYBOARD_QWERTY;
  1400. }
  1401. DisplayServerX11::Property DisplayServerX11::_read_property(Display *p_display, Window p_window, Atom p_property) {
  1402. Atom actual_type;
  1403. int actual_format;
  1404. unsigned long nitems;
  1405. unsigned long bytes_after;
  1406. unsigned char *ret = 0;
  1407. int read_bytes = 1024;
  1408. //Keep trying to read the property until there are no
  1409. //bytes unread.
  1410. do {
  1411. if (ret != 0)
  1412. XFree(ret);
  1413. XGetWindowProperty(p_display, p_window, p_property, 0, read_bytes, False, AnyPropertyType,
  1414. &actual_type, &actual_format, &nitems, &bytes_after,
  1415. &ret);
  1416. read_bytes *= 2;
  1417. } while (bytes_after != 0);
  1418. Property p = { ret, actual_format, (int)nitems, actual_type };
  1419. return p;
  1420. }
  1421. static Atom pick_target_from_list(Display *p_display, Atom *p_list, int p_count) {
  1422. static const char *target_type = "text/uri-list";
  1423. for (int i = 0; i < p_count; i++) {
  1424. Atom atom = p_list[i];
  1425. if (atom != None && String(XGetAtomName(p_display, atom)) == target_type)
  1426. return atom;
  1427. }
  1428. return None;
  1429. }
  1430. static Atom pick_target_from_atoms(Display *p_disp, Atom p_t1, Atom p_t2, Atom p_t3) {
  1431. static const char *target_type = "text/uri-list";
  1432. if (p_t1 != None && String(XGetAtomName(p_disp, p_t1)) == target_type)
  1433. return p_t1;
  1434. if (p_t2 != None && String(XGetAtomName(p_disp, p_t2)) == target_type)
  1435. return p_t2;
  1436. if (p_t3 != None && String(XGetAtomName(p_disp, p_t3)) == target_type)
  1437. return p_t3;
  1438. return None;
  1439. }
  1440. void DisplayServerX11::_get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state) {
  1441. state->set_shift((p_x11_state & ShiftMask));
  1442. state->set_control((p_x11_state & ControlMask));
  1443. state->set_alt((p_x11_state & Mod1Mask /*|| p_x11_state&Mod5Mask*/)); //altgr should not count as alt
  1444. state->set_metakey((p_x11_state & Mod4Mask));
  1445. }
  1446. unsigned int DisplayServerX11::_get_mouse_button_state(unsigned int p_x11_button, int p_x11_type) {
  1447. unsigned int mask = 1 << (p_x11_button - 1);
  1448. if (p_x11_type == ButtonPress) {
  1449. last_button_state |= mask;
  1450. } else {
  1451. last_button_state &= ~mask;
  1452. }
  1453. return last_button_state;
  1454. }
  1455. void DisplayServerX11::_handle_key_event(WindowID p_window, XKeyEvent *p_event, bool p_echo) {
  1456. WindowData wd = windows[p_window];
  1457. // X11 functions don't know what const is
  1458. XKeyEvent *xkeyevent = p_event;
  1459. // This code was pretty difficult to write.
  1460. // The docs stink and every toolkit seems to
  1461. // do it in a different way.
  1462. /* Phase 1, obtain a proper keysym */
  1463. // This was also very difficult to figure out.
  1464. // You'd expect you could just use Keysym provided by
  1465. // XKeycodeToKeysym to obtain internationalized
  1466. // input.. WRONG!!
  1467. // you must use XLookupString (???) which not only wastes
  1468. // cycles generating an unnecessary string, but also
  1469. // still works in half the cases. (won't handle deadkeys)
  1470. // For more complex input methods (deadkeys and more advanced)
  1471. // you have to use XmbLookupString (??).
  1472. // So.. then you have to chosse which of both results
  1473. // you want to keep.
  1474. // This is a real bizarreness and cpu waster.
  1475. KeySym keysym_keycode = 0; // keysym used to find a keycode
  1476. KeySym keysym_unicode = 0; // keysym used to find unicode
  1477. // XLookupString returns keysyms usable as nice keycodes.
  1478. char str[256 + 1];
  1479. XKeyEvent xkeyevent_no_mod = *xkeyevent;
  1480. xkeyevent_no_mod.state &= ~ShiftMask;
  1481. xkeyevent_no_mod.state &= ~ControlMask;
  1482. XLookupString(xkeyevent, str, 256, &keysym_unicode, NULL);
  1483. XLookupString(&xkeyevent_no_mod, NULL, 0, &keysym_keycode, NULL);
  1484. // Meanwhile, XLookupString returns keysyms useful for unicode.
  1485. if (!xmbstring) {
  1486. // keep a temporary buffer for the string
  1487. xmbstring = (char *)memalloc(sizeof(char) * 8);
  1488. xmblen = 8;
  1489. }
  1490. if (xkeyevent->type == KeyPress && wd.xic) {
  1491. Status status;
  1492. #ifdef X_HAVE_UTF8_STRING
  1493. int utf8len = 8;
  1494. char *utf8string = (char *)memalloc(sizeof(char) * utf8len);
  1495. int utf8bytes = Xutf8LookupString(wd.xic, xkeyevent, utf8string,
  1496. utf8len - 1, &keysym_unicode, &status);
  1497. if (status == XBufferOverflow) {
  1498. utf8len = utf8bytes + 1;
  1499. utf8string = (char *)memrealloc(utf8string, utf8len);
  1500. utf8bytes = Xutf8LookupString(wd.xic, xkeyevent, utf8string,
  1501. utf8len - 1, &keysym_unicode, &status);
  1502. }
  1503. utf8string[utf8bytes] = '\0';
  1504. if (status == XLookupChars) {
  1505. bool keypress = xkeyevent->type == KeyPress;
  1506. unsigned int keycode = KeyMappingX11::get_keycode(keysym_keycode);
  1507. unsigned int physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode);
  1508. if (keycode >= 'a' && keycode <= 'z')
  1509. keycode -= 'a' - 'A';
  1510. String tmp;
  1511. tmp.parse_utf8(utf8string, utf8bytes);
  1512. for (int i = 0; i < tmp.length(); i++) {
  1513. Ref<InputEventKey> k;
  1514. k.instance();
  1515. if (physical_keycode == 0 && keycode == 0 && tmp[i] == 0) {
  1516. continue;
  1517. }
  1518. if (keycode == 0) {
  1519. keycode = physical_keycode;
  1520. }
  1521. _get_key_modifier_state(xkeyevent->state, k);
  1522. k->set_unicode(tmp[i]);
  1523. k->set_pressed(keypress);
  1524. k->set_keycode(keycode);
  1525. k->set_physical_keycode(physical_keycode);
  1526. k->set_echo(false);
  1527. if (k->get_keycode() == KEY_BACKTAB) {
  1528. //make it consistent across platforms.
  1529. k->set_keycode(KEY_TAB);
  1530. k->set_physical_keycode(KEY_TAB);
  1531. k->set_shift(true);
  1532. }
  1533. InputFilter::get_singleton()->accumulate_input_event(k);
  1534. }
  1535. memfree(utf8string);
  1536. return;
  1537. }
  1538. memfree(utf8string);
  1539. #else
  1540. do {
  1541. int mnbytes = XmbLookupString(xic, xkeyevent, xmbstring, xmblen - 1, &keysym_unicode, &status);
  1542. xmbstring[mnbytes] = '\0';
  1543. if (status == XBufferOverflow) {
  1544. xmblen = mnbytes + 1;
  1545. xmbstring = (char *)memrealloc(xmbstring, xmblen);
  1546. }
  1547. } while (status == XBufferOverflow);
  1548. #endif
  1549. }
  1550. /* Phase 2, obtain a Godot keycode from the keysym */
  1551. // KeyMappingX11 just translated the X11 keysym to a PIGUI
  1552. // keysym, so it works in all platforms the same.
  1553. unsigned int keycode = KeyMappingX11::get_keycode(keysym_keycode);
  1554. unsigned int physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode);
  1555. /* Phase 3, obtain a unicode character from the keysym */
  1556. // KeyMappingX11 also translates keysym to unicode.
  1557. // It does a binary search on a table to translate
  1558. // most properly.
  1559. unsigned int unicode = keysym_unicode > 0 ? KeyMappingX11::get_unicode_from_keysym(keysym_unicode) : 0;
  1560. /* Phase 4, determine if event must be filtered */
  1561. // This seems to be a side-effect of using XIM.
  1562. // XEventFilter looks like a core X11 function,
  1563. // but it's actually just used to see if we must
  1564. // ignore a deadkey, or events XIM determines
  1565. // must not reach the actual gui.
  1566. // Guess it was a design problem of the extension
  1567. bool keypress = xkeyevent->type == KeyPress;
  1568. if (physical_keycode == 0 && keycode == 0 && unicode == 0) {
  1569. return;
  1570. }
  1571. if (keycode == 0) {
  1572. keycode = physical_keycode;
  1573. }
  1574. /* Phase 5, determine modifier mask */
  1575. // No problems here, except I had no way to
  1576. // know Mod1 was ALT and Mod4 was META (applekey/winkey)
  1577. // just tried Mods until i found them.
  1578. //print_verbose("mod1: "+itos(xkeyevent->state&Mod1Mask)+" mod 5: "+itos(xkeyevent->state&Mod5Mask));
  1579. Ref<InputEventKey> k;
  1580. k.instance();
  1581. _get_key_modifier_state(xkeyevent->state, k);
  1582. /* Phase 6, determine echo character */
  1583. // Echo characters in X11 are a keyrelease and a keypress
  1584. // one after the other with the (almot) same timestamp.
  1585. // To detect them, i use XPeekEvent and check that their
  1586. // difference in time is below a threshold.
  1587. if (xkeyevent->type != KeyPress) {
  1588. p_echo = false;
  1589. // make sure there are events pending,
  1590. // so this call won't block.
  1591. if (XPending(x11_display) > 0) {
  1592. XEvent peek_event;
  1593. XPeekEvent(x11_display, &peek_event);
  1594. // I'm using a threshold of 5 msecs,
  1595. // since sometimes there seems to be a little
  1596. // jitter. I'm still not convinced that all this approach
  1597. // is correct, but the xorg developers are
  1598. // not very helpful today.
  1599. #define ABSDIFF(x, y) (((x) < (y)) ? ((y) - (x)) : ((x) - (y)))
  1600. ::Time threshold = ABSDIFF(peek_event.xkey.time, xkeyevent->time);
  1601. #undef ABSDIFF
  1602. if (peek_event.type == KeyPress && threshold < 5) {
  1603. KeySym rk;
  1604. XLookupString((XKeyEvent *)&peek_event, str, 256, &rk, NULL);
  1605. if (rk == keysym_keycode) {
  1606. XEvent event;
  1607. XNextEvent(x11_display, &event); //erase next event
  1608. _handle_key_event(p_window, (XKeyEvent *)&event, true);
  1609. return; //ignore current, echo next
  1610. }
  1611. }
  1612. // use the time from peek_event so it always works
  1613. }
  1614. // save the time to check for echo when keypress happens
  1615. }
  1616. /* Phase 7, send event to Window */
  1617. k->set_pressed(keypress);
  1618. if (keycode >= 'a' && keycode <= 'z')
  1619. keycode -= 'a' - 'A';
  1620. k->set_keycode(keycode);
  1621. k->set_physical_keycode(physical_keycode);
  1622. k->set_unicode(unicode);
  1623. k->set_echo(p_echo);
  1624. if (k->get_keycode() == KEY_BACKTAB) {
  1625. //make it consistent across platforms.
  1626. k->set_keycode(KEY_TAB);
  1627. k->set_physical_keycode(KEY_TAB);
  1628. k->set_shift(true);
  1629. }
  1630. //don't set mod state if modifier keys are released by themselves
  1631. //else event.is_action() will not work correctly here
  1632. if (!k->is_pressed()) {
  1633. if (k->get_keycode() == KEY_SHIFT)
  1634. k->set_shift(false);
  1635. else if (k->get_keycode() == KEY_CONTROL)
  1636. k->set_control(false);
  1637. else if (k->get_keycode() == KEY_ALT)
  1638. k->set_alt(false);
  1639. else if (k->get_keycode() == KEY_META)
  1640. k->set_metakey(false);
  1641. }
  1642. bool last_is_pressed = InputFilter::get_singleton()->is_key_pressed(k->get_keycode());
  1643. if (k->is_pressed()) {
  1644. if (last_is_pressed) {
  1645. k->set_echo(true);
  1646. }
  1647. }
  1648. InputFilter::get_singleton()->accumulate_input_event(k);
  1649. }
  1650. void DisplayServerX11::_xim_destroy_callback(::XIM im, ::XPointer client_data,
  1651. ::XPointer call_data) {
  1652. WARN_PRINT("Input method stopped");
  1653. DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data);
  1654. ds->xim = NULL;
  1655. for (Map<WindowID, WindowData>::Element *E = ds->windows.front(); E; E = E->next()) {
  1656. E->get().xic = NULL;
  1657. }
  1658. }
  1659. void DisplayServerX11::_window_changed(XEvent *event) {
  1660. WindowID window_id = MAIN_WINDOW_ID;
  1661. //assign the event to the relevant window
  1662. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1663. if (event->xany.window == E->get().x11_window) {
  1664. window_id = E->key();
  1665. break;
  1666. }
  1667. }
  1668. WindowData &wd = windows[window_id];
  1669. if (wd.xic) {
  1670. // Not portable.
  1671. window_set_ime_position(Point2(0, 1));
  1672. }
  1673. if ((event->xconfigure.width == wd.size.width) &&
  1674. (event->xconfigure.height == wd.size.height))
  1675. return;
  1676. wd.size.width = event->xconfigure.width;
  1677. wd.size.height = event->xconfigure.height;
  1678. #if defined(VULKAN_ENABLED)
  1679. if (rendering_driver == "vulkan") {
  1680. context_vulkan->window_resize(window_id, wd.size.width, wd.size.height);
  1681. }
  1682. #endif
  1683. if (!wd.resize_callback.is_null()) {
  1684. Variant size = wd.size;
  1685. Variant *sizep = &size;
  1686. Variant ret;
  1687. Callable::CallError ce;
  1688. wd.resize_callback.call((const Variant **)&sizep, 1, ret, ce);
  1689. }
  1690. }
  1691. void DisplayServerX11::_dispatch_input_events(const Ref<InputEvent> &p_event) {
  1692. ((DisplayServerX11 *)(get_singleton()))->_dispatch_input_event(p_event);
  1693. }
  1694. void DisplayServerX11::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  1695. Variant ev = p_event;
  1696. Variant *evp = &ev;
  1697. Variant ret;
  1698. Callable::CallError ce;
  1699. Ref<InputEventFromWindow> event_from_window = p_event;
  1700. if (event_from_window.is_valid() && event_from_window->get_window_id() != INVALID_WINDOW_ID) {
  1701. //send to a window
  1702. ERR_FAIL_COND(!windows.has(event_from_window->get_window_id()));
  1703. Callable callable = windows[event_from_window->get_window_id()].input_event_callback;
  1704. if (callable.is_null()) {
  1705. return;
  1706. }
  1707. callable.call((const Variant **)&evp, 1, ret, ce);
  1708. } else {
  1709. //send to all windows
  1710. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1711. Callable callable = E->get().input_event_callback;
  1712. if (callable.is_null()) {
  1713. continue;
  1714. }
  1715. callable.call((const Variant **)&evp, 1, ret, ce);
  1716. }
  1717. }
  1718. }
  1719. void DisplayServerX11::_send_window_event(const WindowData &wd, WindowEvent p_event) {
  1720. if (!wd.event_callback.is_null()) {
  1721. Variant event = int(p_event);
  1722. Variant *eventp = &event;
  1723. Variant ret;
  1724. Callable::CallError ce;
  1725. wd.event_callback.call((const Variant **)&eventp, 1, ret, ce);
  1726. }
  1727. }
  1728. void DisplayServerX11::process_events() {
  1729. do_mouse_warp = false;
  1730. // Is the current mouse mode one where it needs to be grabbed.
  1731. bool mouse_mode_grab = mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED;
  1732. while (XPending(x11_display) > 0) {
  1733. XEvent event;
  1734. XNextEvent(x11_display, &event);
  1735. WindowID window_id = MAIN_WINDOW_ID;
  1736. //assign the event to the relevant window
  1737. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1738. if (event.xany.window == E->get().x11_window) {
  1739. window_id = E->key();
  1740. break;
  1741. }
  1742. }
  1743. if (XFilterEvent(&event, None)) {
  1744. continue;
  1745. }
  1746. if (XGetEventData(x11_display, &event.xcookie)) {
  1747. if (event.xcookie.type == GenericEvent && event.xcookie.extension == xi.opcode) {
  1748. XIDeviceEvent *event_data = (XIDeviceEvent *)event.xcookie.data;
  1749. int index = event_data->detail;
  1750. Vector2 pos = Vector2(event_data->event_x, event_data->event_y);
  1751. switch (event_data->evtype) {
  1752. case XI_HierarchyChanged:
  1753. case XI_DeviceChanged: {
  1754. _refresh_device_info();
  1755. } break;
  1756. case XI_RawMotion: {
  1757. XIRawEvent *raw_event = (XIRawEvent *)event_data;
  1758. int device_id = raw_event->deviceid;
  1759. // Determine the axis used (called valuators in XInput for some forsaken reason)
  1760. // Mask is a bitmask indicating which axes are involved.
  1761. // We are interested in the values of axes 0 and 1.
  1762. if (raw_event->valuators.mask_len <= 0) {
  1763. break;
  1764. }
  1765. const double *values = raw_event->raw_values;
  1766. double rel_x = 0.0;
  1767. double rel_y = 0.0;
  1768. double pressure = 0.0;
  1769. double tilt_x = 0.0;
  1770. double tilt_y = 0.0;
  1771. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_ABSX)) {
  1772. rel_x = *values;
  1773. values++;
  1774. }
  1775. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_ABSY)) {
  1776. rel_y = *values;
  1777. values++;
  1778. }
  1779. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_PRESSURE)) {
  1780. pressure = *values;
  1781. values++;
  1782. }
  1783. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_TILTX)) {
  1784. tilt_x = *values;
  1785. values++;
  1786. }
  1787. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_TILTY)) {
  1788. tilt_y = *values;
  1789. }
  1790. Map<int, Vector3>::Element *pen_info = xi.pen_devices.find(device_id);
  1791. if (pen_info) {
  1792. Vector3 mult = pen_info->value();
  1793. if (mult.x != 0.0) xi.pressure = pressure / mult.x;
  1794. if ((mult.y != 0.0) && (mult.z != 0.0)) xi.tilt = Vector2(tilt_x / mult.y, tilt_y / mult.z);
  1795. }
  1796. // https://bugs.freedesktop.org/show_bug.cgi?id=71609
  1797. // http://lists.libsdl.org/pipermail/commits-libsdl.org/2015-June/000282.html
  1798. if (raw_event->time == xi.last_relative_time && rel_x == xi.relative_motion.x && rel_y == xi.relative_motion.y) {
  1799. break; // Flush duplicate to avoid overly fast motion
  1800. }
  1801. xi.old_raw_pos.x = xi.raw_pos.x;
  1802. xi.old_raw_pos.y = xi.raw_pos.y;
  1803. xi.raw_pos.x = rel_x;
  1804. xi.raw_pos.y = rel_y;
  1805. Map<int, Vector2>::Element *abs_info = xi.absolute_devices.find(device_id);
  1806. if (abs_info) {
  1807. // Absolute mode device
  1808. Vector2 mult = abs_info->value();
  1809. xi.relative_motion.x += (xi.raw_pos.x - xi.old_raw_pos.x) * mult.x;
  1810. xi.relative_motion.y += (xi.raw_pos.y - xi.old_raw_pos.y) * mult.y;
  1811. } else {
  1812. // Relative mode device
  1813. xi.relative_motion.x = xi.raw_pos.x;
  1814. xi.relative_motion.y = xi.raw_pos.y;
  1815. }
  1816. xi.last_relative_time = raw_event->time;
  1817. } break;
  1818. #ifdef TOUCH_ENABLED
  1819. case XI_TouchBegin: // Fall-through
  1820. // Disabled hand-in-hand with the grabbing
  1821. //XIAllowTouchEvents(x11_display, event_data->deviceid, event_data->detail, x11_window, XIAcceptTouch);
  1822. case XI_TouchEnd: {
  1823. bool is_begin = event_data->evtype == XI_TouchBegin;
  1824. Ref<InputEventScreenTouch> st;
  1825. st.instance();
  1826. st->set_window_id(window_id);
  1827. st->set_index(index);
  1828. st->set_position(pos);
  1829. st->set_pressed(is_begin);
  1830. if (is_begin) {
  1831. if (xi.state.has(index)) // Defensive
  1832. break;
  1833. xi.state[index] = pos;
  1834. if (xi.state.size() == 1) {
  1835. // X11 may send a motion event when a touch gesture begins, that would result
  1836. // in a spurious mouse motion event being sent to Godot; remember it to be able to filter it out
  1837. xi.mouse_pos_to_filter = pos;
  1838. }
  1839. InputFilter::get_singleton()->accumulate_input_event(st);
  1840. } else {
  1841. if (!xi.state.has(index)) // Defensive
  1842. break;
  1843. xi.state.erase(index);
  1844. InputFilter::get_singleton()->accumulate_input_event(st);
  1845. }
  1846. } break;
  1847. case XI_TouchUpdate: {
  1848. Map<int, Vector2>::Element *curr_pos_elem = xi.state.find(index);
  1849. if (!curr_pos_elem) { // Defensive
  1850. break;
  1851. }
  1852. if (curr_pos_elem->value() != pos) {
  1853. Ref<InputEventScreenDrag> sd;
  1854. sd.instance();
  1855. sd->set_window_id(window_id);
  1856. sd->set_index(index);
  1857. sd->set_position(pos);
  1858. sd->set_relative(pos - curr_pos_elem->value());
  1859. InputFilter::get_singleton()->accumulate_input_event(sd);
  1860. curr_pos_elem->value() = pos;
  1861. }
  1862. } break;
  1863. #endif
  1864. }
  1865. }
  1866. }
  1867. XFreeEventData(x11_display, &event.xcookie);
  1868. switch (event.type) {
  1869. case Expose:
  1870. Main::force_redraw();
  1871. break;
  1872. case NoExpose:
  1873. minimized = true;
  1874. break;
  1875. case VisibilityNotify: {
  1876. XVisibilityEvent *visibility = (XVisibilityEvent *)&event;
  1877. minimized = (visibility->state == VisibilityFullyObscured);
  1878. } break;
  1879. case LeaveNotify: {
  1880. if (!mouse_mode_grab) {
  1881. _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_EXIT);
  1882. }
  1883. } break;
  1884. case EnterNotify: {
  1885. if (!mouse_mode_grab) {
  1886. _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER);
  1887. }
  1888. } break;
  1889. case FocusIn:
  1890. minimized = false;
  1891. window_has_focus = true;
  1892. _send_window_event(windows[window_id], WINDOW_EVENT_FOCUS_IN);
  1893. window_focused = true;
  1894. if (mouse_mode_grab) {
  1895. // Show and update the cursor if confined and the window regained focus.
  1896. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1897. if (mouse_mode == MOUSE_MODE_CONFINED)
  1898. XUndefineCursor(x11_display, E->get().x11_window);
  1899. else if (mouse_mode == MOUSE_MODE_CAPTURED) // or re-hide it in captured mode
  1900. XDefineCursor(x11_display, E->get().x11_window, null_cursor);
  1901. XGrabPointer(
  1902. x11_display, E->get().x11_window, True,
  1903. ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
  1904. GrabModeAsync, GrabModeAsync, E->get().x11_window, None, CurrentTime);
  1905. }
  1906. }
  1907. #ifdef TOUCH_ENABLED
  1908. // Grab touch devices to avoid OS gesture interference
  1909. /*for (int i = 0; i < xi.touch_devices.size(); ++i) {
  1910. XIGrabDevice(x11_display, xi.touch_devices[i], x11_window, CurrentTime, None, XIGrabModeAsync, XIGrabModeAsync, False, &xi.touch_event_mask);
  1911. }*/
  1912. #endif
  1913. if (windows[window_id].xic) {
  1914. XSetICFocus(windows[window_id].xic);
  1915. }
  1916. break;
  1917. case FocusOut:
  1918. window_has_focus = false;
  1919. InputFilter::get_singleton()->release_pressed_events();
  1920. _send_window_event(windows[window_id], WINDOW_EVENT_FOCUS_OUT);
  1921. window_focused = false;
  1922. if (mouse_mode_grab) {
  1923. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  1924. //dear X11, I try, I really try, but you never work, you do whathever you want.
  1925. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  1926. // Show the cursor if we're in captured mode so it doesn't look weird.
  1927. XUndefineCursor(x11_display, E->get().x11_window);
  1928. }
  1929. }
  1930. XUngrabPointer(x11_display, CurrentTime);
  1931. }
  1932. #ifdef TOUCH_ENABLED
  1933. // Ungrab touch devices so input works as usual while we are unfocused
  1934. /*for (int i = 0; i < xi.touch_devices.size(); ++i) {
  1935. XIUngrabDevice(x11_display, xi.touch_devices[i], CurrentTime);
  1936. }*/
  1937. // Release every pointer to avoid sticky points
  1938. for (Map<int, Vector2>::Element *E = xi.state.front(); E; E = E->next()) {
  1939. Ref<InputEventScreenTouch> st;
  1940. st.instance();
  1941. st->set_index(E->key());
  1942. st->set_window_id(window_id);
  1943. st->set_position(E->get());
  1944. InputFilter::get_singleton()->accumulate_input_event(st);
  1945. }
  1946. xi.state.clear();
  1947. #endif
  1948. if (windows[window_id].xic) {
  1949. XSetICFocus(windows[window_id].xic);
  1950. }
  1951. break;
  1952. case ConfigureNotify:
  1953. _window_changed(&event);
  1954. break;
  1955. case ButtonPress:
  1956. case ButtonRelease: {
  1957. /* exit in case of a mouse button press */
  1958. last_timestamp = event.xbutton.time;
  1959. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  1960. event.xbutton.x = last_mouse_pos.x;
  1961. event.xbutton.y = last_mouse_pos.y;
  1962. }
  1963. Ref<InputEventMouseButton> mb;
  1964. mb.instance();
  1965. mb->set_window_id(window_id);
  1966. _get_key_modifier_state(event.xbutton.state, mb);
  1967. mb->set_button_index(event.xbutton.button);
  1968. if (mb->get_button_index() == 2)
  1969. mb->set_button_index(3);
  1970. else if (mb->get_button_index() == 3)
  1971. mb->set_button_index(2);
  1972. mb->set_button_mask(_get_mouse_button_state(mb->get_button_index(), event.xbutton.type));
  1973. mb->set_position(Vector2(event.xbutton.x, event.xbutton.y));
  1974. mb->set_global_position(mb->get_position());
  1975. mb->set_pressed((event.type == ButtonPress));
  1976. if (event.type == ButtonPress) {
  1977. uint64_t diff = OS::get_singleton()->get_ticks_usec() / 1000 - last_click_ms;
  1978. if (mb->get_button_index() == last_click_button_index) {
  1979. if (diff < 400 && Vector2(last_click_pos).distance_to(Vector2(event.xbutton.x, event.xbutton.y)) < 5) {
  1980. last_click_ms = 0;
  1981. last_click_pos = Point2i(-100, -100);
  1982. last_click_button_index = -1;
  1983. mb->set_doubleclick(true);
  1984. }
  1985. } else if (mb->get_button_index() < 4 || mb->get_button_index() > 7) {
  1986. last_click_button_index = mb->get_button_index();
  1987. }
  1988. if (!mb->is_doubleclick()) {
  1989. last_click_ms += diff;
  1990. last_click_pos = Point2i(event.xbutton.x, event.xbutton.y);
  1991. }
  1992. }
  1993. InputFilter::get_singleton()->accumulate_input_event(mb);
  1994. } break;
  1995. case MotionNotify: {
  1996. // The X11 API requires filtering one-by-one through the motion
  1997. // notify events, in order to figure out which event is the one
  1998. // generated by warping the mouse pointer.
  1999. while (true) {
  2000. if (mouse_mode == MOUSE_MODE_CAPTURED && event.xmotion.x == windows[MAIN_WINDOW_ID].size.width / 2 && event.xmotion.y == windows[MAIN_WINDOW_ID].size.height / 2) {
  2001. //this is likely the warp event since it was warped here
  2002. center = Vector2(event.xmotion.x, event.xmotion.y);
  2003. break;
  2004. }
  2005. if (XPending(x11_display) > 0) {
  2006. XEvent tevent;
  2007. XPeekEvent(x11_display, &tevent);
  2008. if (tevent.type == MotionNotify) {
  2009. XNextEvent(x11_display, &event);
  2010. } else {
  2011. break;
  2012. }
  2013. } else {
  2014. break;
  2015. }
  2016. }
  2017. last_timestamp = event.xmotion.time;
  2018. // Motion is also simple.
  2019. // A little hack is in order
  2020. // to be able to send relative motion events.
  2021. Point2i pos(event.xmotion.x, event.xmotion.y);
  2022. // Avoidance of spurious mouse motion (see handling of touch)
  2023. bool filter = false;
  2024. // Adding some tolerance to match better Point2i to Vector2
  2025. if (xi.state.size() && Vector2(pos).distance_squared_to(xi.mouse_pos_to_filter) < 2) {
  2026. filter = true;
  2027. }
  2028. // Invalidate to avoid filtering a possible legitimate similar event coming later
  2029. xi.mouse_pos_to_filter = Vector2(1e10, 1e10);
  2030. if (filter) {
  2031. break;
  2032. }
  2033. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  2034. if (xi.relative_motion.x == 0 && xi.relative_motion.y == 0) {
  2035. break;
  2036. }
  2037. Point2i new_center = pos;
  2038. pos = last_mouse_pos + xi.relative_motion;
  2039. center = new_center;
  2040. do_mouse_warp = window_has_focus; // warp the cursor if we're focused in
  2041. }
  2042. if (!last_mouse_pos_valid) {
  2043. last_mouse_pos = pos;
  2044. last_mouse_pos_valid = true;
  2045. }
  2046. // Hackish but relative mouse motion is already handled in the RawMotion event.
  2047. // RawMotion does not provide the absolute mouse position (whereas MotionNotify does).
  2048. // Therefore, RawMotion cannot be the authority on absolute mouse position.
  2049. // RawMotion provides more precision than MotionNotify, which doesn't sense subpixel motion.
  2050. // Therefore, MotionNotify cannot be the authority on relative mouse motion.
  2051. // This means we need to take a combined approach...
  2052. Point2i rel;
  2053. // Only use raw input if in capture mode. Otherwise use the classic behavior.
  2054. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  2055. rel = xi.relative_motion;
  2056. } else {
  2057. rel = pos - last_mouse_pos;
  2058. }
  2059. // Reset to prevent lingering motion
  2060. xi.relative_motion.x = 0;
  2061. xi.relative_motion.y = 0;
  2062. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  2063. pos = Point2i(windows[MAIN_WINDOW_ID].size.width / 2, windows[MAIN_WINDOW_ID].size.height / 2);
  2064. }
  2065. Ref<InputEventMouseMotion> mm;
  2066. mm.instance();
  2067. mm->set_window_id(window_id);
  2068. mm->set_pressure(xi.pressure);
  2069. mm->set_tilt(xi.tilt);
  2070. // Make the absolute position integral so it doesn't look _too_ weird :)
  2071. Point2i posi(pos);
  2072. _get_key_modifier_state(event.xmotion.state, mm);
  2073. mm->set_button_mask(mouse_get_button_state());
  2074. mm->set_position(posi);
  2075. mm->set_global_position(posi);
  2076. InputFilter::get_singleton()->set_mouse_position(posi);
  2077. mm->set_speed(InputFilter::get_singleton()->get_last_mouse_speed());
  2078. mm->set_relative(rel);
  2079. last_mouse_pos = pos;
  2080. // printf("rel: %d,%d\n", rel.x, rel.y );
  2081. // Don't propagate the motion event unless we have focus
  2082. // this is so that the relative motion doesn't get messed up
  2083. // after we regain focus.
  2084. if (window_has_focus || !mouse_mode_grab)
  2085. InputFilter::get_singleton()->accumulate_input_event(mm);
  2086. } break;
  2087. case KeyPress:
  2088. case KeyRelease: {
  2089. last_timestamp = event.xkey.time;
  2090. // key event is a little complex, so
  2091. // it will be handled in its own function.
  2092. _handle_key_event(window_id, (XKeyEvent *)&event);
  2093. } break;
  2094. case SelectionRequest: {
  2095. XSelectionRequestEvent *req;
  2096. XEvent e, respond;
  2097. e = event;
  2098. req = &(e.xselectionrequest);
  2099. if (req->target == XInternAtom(x11_display, "UTF8_STRING", 0) ||
  2100. req->target == XInternAtom(x11_display, "COMPOUND_TEXT", 0) ||
  2101. req->target == XInternAtom(x11_display, "TEXT", 0) ||
  2102. req->target == XA_STRING ||
  2103. req->target == XInternAtom(x11_display, "text/plain;charset=utf-8", 0) ||
  2104. req->target == XInternAtom(x11_display, "text/plain", 0)) {
  2105. CharString clip = clipboard_get().utf8();
  2106. XChangeProperty(x11_display,
  2107. req->requestor,
  2108. req->property,
  2109. req->target,
  2110. 8,
  2111. PropModeReplace,
  2112. (unsigned char *)clip.get_data(),
  2113. clip.length());
  2114. respond.xselection.property = req->property;
  2115. } else if (req->target == XInternAtom(x11_display, "TARGETS", 0)) {
  2116. Atom data[7];
  2117. data[0] = XInternAtom(x11_display, "TARGETS", 0);
  2118. data[1] = XInternAtom(x11_display, "UTF8_STRING", 0);
  2119. data[2] = XInternAtom(x11_display, "COMPOUND_TEXT", 0);
  2120. data[3] = XInternAtom(x11_display, "TEXT", 0);
  2121. data[4] = XA_STRING;
  2122. data[5] = XInternAtom(x11_display, "text/plain;charset=utf-8", 0);
  2123. data[6] = XInternAtom(x11_display, "text/plain", 0);
  2124. XChangeProperty(x11_display,
  2125. req->requestor,
  2126. req->property,
  2127. XA_ATOM,
  2128. 32,
  2129. PropModeReplace,
  2130. (unsigned char *)&data,
  2131. sizeof(data) / sizeof(data[0]));
  2132. respond.xselection.property = req->property;
  2133. } else {
  2134. char *targetname = XGetAtomName(x11_display, req->target);
  2135. printf("No Target '%s'\n", targetname);
  2136. if (targetname)
  2137. XFree(targetname);
  2138. respond.xselection.property = None;
  2139. }
  2140. respond.xselection.type = SelectionNotify;
  2141. respond.xselection.display = req->display;
  2142. respond.xselection.requestor = req->requestor;
  2143. respond.xselection.selection = req->selection;
  2144. respond.xselection.target = req->target;
  2145. respond.xselection.time = req->time;
  2146. XSendEvent(x11_display, req->requestor, True, NoEventMask, &respond);
  2147. XFlush(x11_display);
  2148. } break;
  2149. case SelectionNotify:
  2150. if (event.xselection.target == requested) {
  2151. Property p = _read_property(x11_display, windows[window_id].x11_window, XInternAtom(x11_display, "PRIMARY", 0));
  2152. Vector<String> files = String((char *)p.data).split("\n", false);
  2153. for (int i = 0; i < files.size(); i++) {
  2154. files.write[i] = files[i].replace("file://", "").http_unescape().strip_edges();
  2155. }
  2156. if (!windows[window_id].drop_files_callback.is_null()) {
  2157. Variant v = files;
  2158. Variant *vp = &v;
  2159. Variant ret;
  2160. Callable::CallError ce;
  2161. windows[window_id].drop_files_callback.call((const Variant **)&vp, 1, ret, ce);
  2162. }
  2163. //Reply that all is well.
  2164. XClientMessageEvent m;
  2165. memset(&m, 0, sizeof(m));
  2166. m.type = ClientMessage;
  2167. m.display = x11_display;
  2168. m.window = xdnd_source_window;
  2169. m.message_type = xdnd_finished;
  2170. m.format = 32;
  2171. m.data.l[0] = windows[window_id].x11_window;
  2172. m.data.l[1] = 1;
  2173. m.data.l[2] = xdnd_action_copy; //We only ever copy.
  2174. XSendEvent(x11_display, xdnd_source_window, False, NoEventMask, (XEvent *)&m);
  2175. }
  2176. break;
  2177. case ClientMessage:
  2178. if ((unsigned int)event.xclient.data.l[0] == (unsigned int)wm_delete) {
  2179. _send_window_event(windows[window_id], WINDOW_EVENT_CLOSE_REQUEST);
  2180. }
  2181. else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_enter) {
  2182. //File(s) have been dragged over the window, check for supported target (text/uri-list)
  2183. xdnd_version = (event.xclient.data.l[1] >> 24);
  2184. Window source = event.xclient.data.l[0];
  2185. bool more_than_3 = event.xclient.data.l[1] & 1;
  2186. if (more_than_3) {
  2187. Property p = _read_property(x11_display, source, XInternAtom(x11_display, "XdndTypeList", False));
  2188. requested = pick_target_from_list(x11_display, (Atom *)p.data, p.nitems);
  2189. } else
  2190. requested = pick_target_from_atoms(x11_display, event.xclient.data.l[2], event.xclient.data.l[3], event.xclient.data.l[4]);
  2191. } else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_position) {
  2192. //xdnd position event, reply with an XDND status message
  2193. //just depending on type of data for now
  2194. XClientMessageEvent m;
  2195. memset(&m, 0, sizeof(m));
  2196. m.type = ClientMessage;
  2197. m.display = event.xclient.display;
  2198. m.window = event.xclient.data.l[0];
  2199. m.message_type = xdnd_status;
  2200. m.format = 32;
  2201. m.data.l[0] = windows[window_id].x11_window;
  2202. m.data.l[1] = (requested != None);
  2203. m.data.l[2] = 0; //empty rectangle
  2204. m.data.l[3] = 0;
  2205. m.data.l[4] = xdnd_action_copy;
  2206. XSendEvent(x11_display, event.xclient.data.l[0], False, NoEventMask, (XEvent *)&m);
  2207. XFlush(x11_display);
  2208. } else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_drop) {
  2209. if (requested != None) {
  2210. xdnd_source_window = event.xclient.data.l[0];
  2211. if (xdnd_version >= 1)
  2212. XConvertSelection(x11_display, xdnd_selection, requested, XInternAtom(x11_display, "PRIMARY", 0), windows[window_id].x11_window, event.xclient.data.l[2]);
  2213. else
  2214. XConvertSelection(x11_display, xdnd_selection, requested, XInternAtom(x11_display, "PRIMARY", 0), windows[window_id].x11_window, CurrentTime);
  2215. } else {
  2216. //Reply that we're not interested.
  2217. XClientMessageEvent m;
  2218. memset(&m, 0, sizeof(m));
  2219. m.type = ClientMessage;
  2220. m.display = event.xclient.display;
  2221. m.window = event.xclient.data.l[0];
  2222. m.message_type = xdnd_finished;
  2223. m.format = 32;
  2224. m.data.l[0] = windows[window_id].x11_window;
  2225. m.data.l[1] = 0;
  2226. m.data.l[2] = None; //Failed.
  2227. XSendEvent(x11_display, event.xclient.data.l[0], False, NoEventMask, (XEvent *)&m);
  2228. }
  2229. }
  2230. break;
  2231. default:
  2232. break;
  2233. }
  2234. }
  2235. XFlush(x11_display);
  2236. if (do_mouse_warp) {
  2237. XWarpPointer(x11_display, None, windows[MAIN_WINDOW_ID].x11_window,
  2238. 0, 0, 0, 0, (int)windows[MAIN_WINDOW_ID].size.width / 2, (int)windows[MAIN_WINDOW_ID].size.height / 2);
  2239. /*
  2240. Window root, child;
  2241. int root_x, root_y;
  2242. int win_x, win_y;
  2243. unsigned int mask;
  2244. XQueryPointer( x11_display, x11_window, &root, &child, &root_x, &root_y, &win_x, &win_y, &mask );
  2245. printf("Root: %d,%d\n", root_x, root_y);
  2246. printf("Win: %d,%d\n", win_x, win_y);
  2247. */
  2248. }
  2249. InputFilter::get_singleton()->flush_accumulated_events();
  2250. }
  2251. void DisplayServerX11::release_rendering_thread() {
  2252. }
  2253. void DisplayServerX11::make_rendering_thread() {
  2254. }
  2255. void DisplayServerX11::swap_buffers() {
  2256. }
  2257. void DisplayServerX11::_update_context(WindowData &wd) {
  2258. XClassHint *classHint = XAllocClassHint();
  2259. if (classHint) {
  2260. CharString name_str;
  2261. switch (context) {
  2262. case CONTEXT_EDITOR:
  2263. name_str = "Godot_Editor";
  2264. break;
  2265. case CONTEXT_PROJECTMAN:
  2266. name_str = "Godot_ProjectList";
  2267. break;
  2268. case CONTEXT_ENGINE:
  2269. name_str = "Godot_Engine";
  2270. break;
  2271. }
  2272. CharString class_str;
  2273. if (context == CONTEXT_ENGINE) {
  2274. String config_name = GLOBAL_GET("application/config/name");
  2275. if (config_name.length() == 0) {
  2276. class_str = "Godot_Engine";
  2277. } else {
  2278. class_str = config_name.utf8();
  2279. }
  2280. } else {
  2281. class_str = "Godot";
  2282. }
  2283. classHint->res_class = class_str.ptrw();
  2284. classHint->res_name = name_str.ptrw();
  2285. XSetClassHint(x11_display, wd.x11_window, classHint);
  2286. XFree(classHint);
  2287. }
  2288. }
  2289. void DisplayServerX11::set_context(Context p_context) {
  2290. context = p_context;
  2291. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  2292. _update_context(E->get());
  2293. }
  2294. }
  2295. void DisplayServerX11::set_native_icon(const String &p_filename) {
  2296. WARN_PRINT("Native icon not supported by this display server.");
  2297. }
  2298. void DisplayServerX11::set_icon(const Ref<Image> &p_icon) {
  2299. WARN_PRINT("Icon not supported by this display server.");
  2300. }
  2301. Vector<String> DisplayServerX11::get_rendering_drivers_func() {
  2302. Vector<String> drivers;
  2303. #ifdef VULKAN_ENABLED
  2304. drivers.push_back("vulkan");
  2305. #endif
  2306. #ifdef OPENGL_ENABLED
  2307. drivers.push_back("opengl");
  2308. #endif
  2309. return drivers;
  2310. }
  2311. DisplayServer *DisplayServerX11::create_func(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  2312. return memnew(DisplayServerX11(p_rendering_driver, p_mode, p_flags, p_resolution, r_error));
  2313. }
  2314. DisplayServerX11::WindowID DisplayServerX11::_create_window(WindowMode p_mode, const Vector2i &p_resolution) {
  2315. //Create window
  2316. long visualMask = VisualScreenMask;
  2317. int numberOfVisuals;
  2318. XVisualInfo vInfoTemplate = {};
  2319. vInfoTemplate.screen = DefaultScreen(x11_display);
  2320. XVisualInfo *visualInfo = XGetVisualInfo(x11_display, visualMask, &vInfoTemplate, &numberOfVisuals);
  2321. Colormap colormap = XCreateColormap(x11_display, RootWindow(x11_display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
  2322. XSetWindowAttributes windowAttributes = {};
  2323. windowAttributes.colormap = colormap;
  2324. windowAttributes.background_pixel = 0xFFFFFFFF;
  2325. windowAttributes.border_pixel = 0;
  2326. windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
  2327. unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask;
  2328. WindowID id;
  2329. {
  2330. WindowData wd;
  2331. wd.x11_window = XCreateWindow(x11_display, RootWindow(x11_display, visualInfo->screen), 0, 0, p_resolution.width, p_resolution.height, 0, visualInfo->depth, InputOutput, visualInfo->visual, valuemask, &windowAttributes);
  2332. //set_class_hint(x11_display, wd.x11_window);
  2333. XMapWindow(x11_display, wd.x11_window);
  2334. XFlush(x11_display);
  2335. XSync(x11_display, False);
  2336. //XSetErrorHandler(oldHandler);
  2337. XFree(visualInfo);
  2338. #if defined(VULKAN_ENABLED)
  2339. if (context_vulkan) {
  2340. Error err = context_vulkan->window_create(window_id_counter, wd.x11_window, x11_display, p_resolution.width, p_resolution.height);
  2341. ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create a Vulkan window");
  2342. }
  2343. #endif
  2344. //associate PID
  2345. // make PID known to X11
  2346. {
  2347. const long pid = OS::get_singleton()->get_process_id();
  2348. Atom net_wm_pid = XInternAtom(x11_display, "_NET_WM_PID", False);
  2349. XChangeProperty(x11_display, wd.x11_window, net_wm_pid, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&pid, 1);
  2350. }
  2351. long im_event_mask = 0;
  2352. {
  2353. XIEventMask all_event_mask;
  2354. XSetWindowAttributes new_attr;
  2355. new_attr.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
  2356. ButtonReleaseMask | EnterWindowMask |
  2357. LeaveWindowMask | PointerMotionMask |
  2358. Button1MotionMask |
  2359. Button2MotionMask | Button3MotionMask |
  2360. Button4MotionMask | Button5MotionMask |
  2361. ButtonMotionMask | KeymapStateMask |
  2362. ExposureMask | VisibilityChangeMask |
  2363. StructureNotifyMask |
  2364. SubstructureNotifyMask | SubstructureRedirectMask |
  2365. FocusChangeMask | PropertyChangeMask |
  2366. ColormapChangeMask | OwnerGrabButtonMask |
  2367. im_event_mask;
  2368. XChangeWindowAttributes(x11_display, wd.x11_window, CWEventMask, &new_attr);
  2369. static unsigned char all_mask_data[XIMaskLen(XI_LASTEVENT)] = {};
  2370. all_event_mask.deviceid = XIAllDevices;
  2371. all_event_mask.mask_len = sizeof(all_mask_data);
  2372. all_event_mask.mask = all_mask_data;
  2373. XISetMask(all_event_mask.mask, XI_HierarchyChanged);
  2374. #ifdef TOUCH_ENABLED
  2375. if (xi.touch_devices.size()) {
  2376. XISetMask(all_event_mask.mask, XI_TouchBegin);
  2377. XISetMask(all_event_mask.mask, XI_TouchUpdate);
  2378. XISetMask(all_event_mask.mask, XI_TouchEnd);
  2379. XISetMask(all_event_mask.mask, XI_TouchOwnership);
  2380. }
  2381. #endif
  2382. XISelectEvents(x11_display, wd.x11_window, &all_event_mask, 1);
  2383. }
  2384. /* set the titlebar name */
  2385. XStoreName(x11_display, wd.x11_window, "Godot");
  2386. XSetWMProtocols(x11_display, wd.x11_window, &wm_delete, 1);
  2387. XChangeProperty(x11_display, wd.x11_window, xdnd_aware, XA_ATOM, 32, PropModeReplace, (unsigned char *)&xdnd_version, 1);
  2388. if (xim && xim_style) {
  2389. wd.xic = XCreateIC(xim, XNInputStyle, xim_style, XNClientWindow, wd.x11_window, XNFocusWindow, wd.x11_window, (char *)NULL);
  2390. if (XGetICValues(wd.xic, XNFilterEvents, &im_event_mask, NULL) != NULL) {
  2391. WARN_PRINT("XGetICValues couldn't obtain XNFilterEvents value");
  2392. XDestroyIC(wd.xic);
  2393. wd.xic = NULL;
  2394. }
  2395. if (wd.xic) {
  2396. XUnsetICFocus(wd.xic);
  2397. } else {
  2398. WARN_PRINT("XCreateIC couldn't create wd.xic");
  2399. }
  2400. } else {
  2401. wd.xic = NULL;
  2402. WARN_PRINT("XCreateIC couldn't create wd.xic");
  2403. }
  2404. _update_context(wd);
  2405. id = window_id_counter++;
  2406. windows[id] = wd;
  2407. }
  2408. WindowData &wd = windows[id];
  2409. window_set_mode(p_mode, id);
  2410. //set cursor
  2411. if (cursors[current_cursor] != None) {
  2412. XDefineCursor(x11_display, wd.x11_window, cursors[current_cursor]);
  2413. }
  2414. return id;
  2415. }
  2416. DisplayServerX11::DisplayServerX11(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  2417. InputFilter::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  2418. r_error = OK;
  2419. last_button_state = 0;
  2420. xmbstring = NULL;
  2421. last_click_ms = 0;
  2422. last_click_button_index = -1;
  2423. last_click_pos = Point2i(-100, -100);
  2424. last_timestamp = 0;
  2425. last_mouse_pos_valid = false;
  2426. last_keyrelease_time = 0;
  2427. if (OS::get_singleton()->get_render_thread_mode() == OS::RENDER_SEPARATE_THREAD) {
  2428. XInitThreads();
  2429. }
  2430. /** XLIB INITIALIZATION **/
  2431. x11_display = XOpenDisplay(NULL);
  2432. if (!x11_display) {
  2433. ERR_PRINT("X11 Display is not available");
  2434. r_error = ERR_UNAVAILABLE;
  2435. return;
  2436. }
  2437. char *modifiers = NULL;
  2438. Bool xkb_dar = False;
  2439. XAutoRepeatOn(x11_display);
  2440. xkb_dar = XkbSetDetectableAutoRepeat(x11_display, True, NULL);
  2441. // Try to support IME if detectable auto-repeat is supported
  2442. if (xkb_dar == True) {
  2443. #ifdef X_HAVE_UTF8_STRING
  2444. // Xutf8LookupString will be used later instead of XmbLookupString before
  2445. // the multibyte sequences can be converted to unicode string.
  2446. modifiers = XSetLocaleModifiers("");
  2447. #endif
  2448. }
  2449. if (modifiers == NULL) {
  2450. if (OS::get_singleton()->is_stdout_verbose()) {
  2451. WARN_PRINT("IME is disabled");
  2452. }
  2453. XSetLocaleModifiers("@im=none");
  2454. WARN_PRINT("Error setting locale modifiers");
  2455. }
  2456. const char *err;
  2457. xrr_get_monitors = NULL;
  2458. xrr_free_monitors = NULL;
  2459. int xrandr_major = 0;
  2460. int xrandr_minor = 0;
  2461. int event_base, error_base;
  2462. xrandr_ext_ok = XRRQueryExtension(x11_display, &event_base, &error_base);
  2463. xrandr_handle = dlopen("libXrandr.so.2", RTLD_LAZY);
  2464. if (!xrandr_handle) {
  2465. err = dlerror();
  2466. fprintf(stderr, "could not load libXrandr.so.2, Error: %s\n", err);
  2467. } else {
  2468. XRRQueryVersion(x11_display, &xrandr_major, &xrandr_minor);
  2469. if (((xrandr_major << 8) | xrandr_minor) >= 0x0105) {
  2470. xrr_get_monitors = (xrr_get_monitors_t)dlsym(xrandr_handle, "XRRGetMonitors");
  2471. if (!xrr_get_monitors) {
  2472. err = dlerror();
  2473. fprintf(stderr, "could not find symbol XRRGetMonitors\nError: %s\n", err);
  2474. } else {
  2475. xrr_free_monitors = (xrr_free_monitors_t)dlsym(xrandr_handle, "XRRFreeMonitors");
  2476. if (!xrr_free_monitors) {
  2477. err = dlerror();
  2478. fprintf(stderr, "could not find XRRFreeMonitors\nError: %s\n", err);
  2479. xrr_get_monitors = NULL;
  2480. }
  2481. }
  2482. }
  2483. }
  2484. if (!_refresh_device_info()) {
  2485. alert("Your system does not support XInput 2.\n"
  2486. "Please upgrade your distribution.",
  2487. "Unable to initialize XInput");
  2488. r_error = ERR_UNAVAILABLE;
  2489. return;
  2490. }
  2491. xim = XOpenIM(x11_display, NULL, NULL, NULL);
  2492. if (xim == NULL) {
  2493. WARN_PRINT("XOpenIM failed");
  2494. xim_style = 0L;
  2495. } else {
  2496. ::XIMCallback im_destroy_callback;
  2497. im_destroy_callback.client_data = (::XPointer)(this);
  2498. im_destroy_callback.callback = (::XIMProc)(_xim_destroy_callback);
  2499. if (XSetIMValues(xim, XNDestroyCallback, &im_destroy_callback,
  2500. NULL) != NULL) {
  2501. WARN_PRINT("Error setting XIM destroy callback");
  2502. }
  2503. ::XIMStyles *xim_styles = NULL;
  2504. xim_style = 0L;
  2505. char *imvalret = XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
  2506. if (imvalret != NULL || xim_styles == NULL) {
  2507. fprintf(stderr, "Input method doesn't support any styles\n");
  2508. }
  2509. if (xim_styles) {
  2510. xim_style = 0L;
  2511. for (int i = 0; i < xim_styles->count_styles; i++) {
  2512. if (xim_styles->supported_styles[i] ==
  2513. (XIMPreeditNothing | XIMStatusNothing)) {
  2514. xim_style = xim_styles->supported_styles[i];
  2515. break;
  2516. }
  2517. }
  2518. XFree(xim_styles);
  2519. }
  2520. XFree(imvalret);
  2521. }
  2522. /* Atorm internment */
  2523. wm_delete = XInternAtom(x11_display, "WM_DELETE_WINDOW", true);
  2524. //Set Xdnd (drag & drop) support
  2525. xdnd_aware = XInternAtom(x11_display, "XdndAware", False);
  2526. xdnd_version = 5;
  2527. xdnd_enter = XInternAtom(x11_display, "XdndEnter", False);
  2528. xdnd_position = XInternAtom(x11_display, "XdndPosition", False);
  2529. xdnd_status = XInternAtom(x11_display, "XdndStatus", False);
  2530. xdnd_action_copy = XInternAtom(x11_display, "XdndActionCopy", False);
  2531. xdnd_drop = XInternAtom(x11_display, "XdndDrop", False);
  2532. xdnd_finished = XInternAtom(x11_display, "XdndFinished", False);
  2533. xdnd_selection = XInternAtom(x11_display, "XdndSelection", False);
  2534. //!!!!!!!!!!!!!!!!!!!!!!!!!!
  2535. //TODO - do Vulkan and GLES2 support checks, driver selection and fallback
  2536. rendering_driver = p_rendering_driver;
  2537. #ifndef _MSC_VER
  2538. #warning Forcing vulkan rendering driver because OpenGL not implemented yet
  2539. #endif
  2540. rendering_driver = "vulkan";
  2541. #if defined(VULKAN_ENABLED)
  2542. if (rendering_driver == "vulkan") {
  2543. context_vulkan = memnew(VulkanContextX11);
  2544. if (context_vulkan->initialize() != OK) {
  2545. memdelete(context_vulkan);
  2546. context_vulkan = NULL;
  2547. r_error = ERR_CANT_CREATE;
  2548. ERR_FAIL_MSG("Could not initialize Vulkan");
  2549. }
  2550. }
  2551. #endif
  2552. // Init context and rendering device
  2553. #if defined(OPENGL_ENABLED)
  2554. if (rendering_driver == "opengl_es") {
  2555. if (getenv("DRI_PRIME") == NULL) {
  2556. int use_prime = -1;
  2557. if (getenv("PRIMUS_DISPLAY") ||
  2558. getenv("PRIMUS_libGLd") ||
  2559. getenv("PRIMUS_libGLa") ||
  2560. getenv("PRIMUS_libGL") ||
  2561. getenv("PRIMUS_LOAD_GLOBAL") ||
  2562. getenv("BUMBLEBEE_SOCKET")) {
  2563. print_verbose("Optirun/primusrun detected. Skipping GPU detection");
  2564. use_prime = 0;
  2565. }
  2566. if (getenv("LD_LIBRARY_PATH")) {
  2567. String ld_library_path(getenv("LD_LIBRARY_PATH"));
  2568. Vector<String> libraries = ld_library_path.split(":");
  2569. for (int i = 0; i < libraries.size(); ++i) {
  2570. if (FileAccess::exists(libraries[i] + "/libGL.so.1") ||
  2571. FileAccess::exists(libraries[i] + "/libGL.so")) {
  2572. print_verbose("Custom libGL override detected. Skipping GPU detection");
  2573. use_prime = 0;
  2574. }
  2575. }
  2576. }
  2577. if (use_prime == -1) {
  2578. print_verbose("Detecting GPUs, set DRI_PRIME in the environment to override GPU detection logic.");
  2579. use_prime = detect_prime();
  2580. }
  2581. if (use_prime) {
  2582. print_line("Found discrete GPU, setting DRI_PRIME=1 to use it.");
  2583. print_line("Note: Set DRI_PRIME=0 in the environment to disable Godot from using the discrete GPU.");
  2584. setenv("DRI_PRIME", "1", 1);
  2585. }
  2586. }
  2587. ContextGL_X11::ContextType opengl_api_type = ContextGL_X11::GLES_2_0_COMPATIBLE;
  2588. context_gles2 = memnew(ContextGL_X11(x11_display, x11_window, current_videomode, opengl_api_type));
  2589. if (context_gles2->initialize() != OK) {
  2590. memdelete(context_gles2);
  2591. context_gles2 = NULL;
  2592. ERR_FAIL_V(ERR_UNAVAILABLE);
  2593. }
  2594. context_gles2->set_use_vsync(current_videomode.use_vsync);
  2595. if (RasterizerGLES2::is_viable() == OK) {
  2596. RasterizerGLES2::register_config();
  2597. RasterizerGLES2::make_current();
  2598. } else {
  2599. memdelete(context_gles2);
  2600. context_gles2 = NULL;
  2601. ERR_FAIL_V(ERR_UNAVAILABLE);
  2602. }
  2603. }
  2604. #endif
  2605. WindowID main_window = _create_window(p_mode, p_resolution);
  2606. for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
  2607. if (p_flags & (1 << i)) {
  2608. window_set_flag(WindowFlags(i), true, main_window);
  2609. }
  2610. }
  2611. //create RenderingDevice if used
  2612. #if defined(VULKAN_ENABLED)
  2613. if (rendering_driver == "vulkan") {
  2614. //temporary
  2615. rendering_device_vulkan = memnew(RenderingDeviceVulkan);
  2616. rendering_device_vulkan->initialize(context_vulkan);
  2617. RasterizerRD::make_current();
  2618. }
  2619. #endif
  2620. /*
  2621. visual_server = memnew(VisualServerRaster);
  2622. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  2623. visual_server = memnew(VisualServerWrapMT(visual_server, get_render_thread_mode() == RENDER_SEPARATE_THREAD));
  2624. }
  2625. */
  2626. {
  2627. //set all event master mask
  2628. XIEventMask all_master_event_mask;
  2629. static unsigned char all_master_mask_data[XIMaskLen(XI_LASTEVENT)] = {};
  2630. all_master_event_mask.deviceid = XIAllMasterDevices;
  2631. all_master_event_mask.mask_len = sizeof(all_master_mask_data);
  2632. all_master_event_mask.mask = all_master_mask_data;
  2633. XISetMask(all_master_event_mask.mask, XI_DeviceChanged);
  2634. XISetMask(all_master_event_mask.mask, XI_RawMotion);
  2635. XISelectEvents(x11_display, DefaultRootWindow(x11_display), &all_master_event_mask, 1);
  2636. }
  2637. // Disabled by now since grabbing also blocks mouse events
  2638. // (they are received as extended events instead of standard events)
  2639. /*XIClearMask(xi.touch_event_mask.mask, XI_TouchOwnership);
  2640. // Grab touch devices to avoid OS gesture interference
  2641. for (int i = 0; i < xi.touch_devices.size(); ++i) {
  2642. XIGrabDevice(x11_display, xi.touch_devices[i], x11_window, CurrentTime, None, XIGrabModeAsync, XIGrabModeAsync, False, &xi.touch_event_mask);
  2643. }*/
  2644. cursor_size = XcursorGetDefaultSize(x11_display);
  2645. cursor_theme = XcursorGetTheme(x11_display);
  2646. if (!cursor_theme) {
  2647. print_verbose("XcursorGetTheme could not get cursor theme");
  2648. cursor_theme = "default";
  2649. }
  2650. for (int i = 0; i < CURSOR_MAX; i++) {
  2651. cursors[i] = None;
  2652. img[i] = NULL;
  2653. }
  2654. current_cursor = CURSOR_ARROW;
  2655. for (int i = 0; i < CURSOR_MAX; i++) {
  2656. static const char *cursor_file[] = {
  2657. "left_ptr",
  2658. "xterm",
  2659. "hand2",
  2660. "cross",
  2661. "watch",
  2662. "left_ptr_watch",
  2663. "fleur",
  2664. "dnd-move",
  2665. "crossed_circle",
  2666. "v_double_arrow",
  2667. "h_double_arrow",
  2668. "size_bdiag",
  2669. "size_fdiag",
  2670. "move",
  2671. "row_resize",
  2672. "col_resize",
  2673. "question_arrow"
  2674. };
  2675. img[i] = XcursorLibraryLoadImage(cursor_file[i], cursor_theme, cursor_size);
  2676. if (!img[i]) {
  2677. const char *fallback = NULL;
  2678. switch (i) {
  2679. case CURSOR_POINTING_HAND:
  2680. fallback = "pointer";
  2681. break;
  2682. case CURSOR_CROSS:
  2683. fallback = "crosshair";
  2684. break;
  2685. case CURSOR_WAIT:
  2686. fallback = "wait";
  2687. break;
  2688. case CURSOR_BUSY:
  2689. fallback = "progress";
  2690. break;
  2691. case CURSOR_DRAG:
  2692. fallback = "grabbing";
  2693. break;
  2694. case CURSOR_CAN_DROP:
  2695. fallback = "hand1";
  2696. break;
  2697. case CURSOR_FORBIDDEN:
  2698. fallback = "forbidden";
  2699. break;
  2700. case CURSOR_VSIZE:
  2701. fallback = "ns-resize";
  2702. break;
  2703. case CURSOR_HSIZE:
  2704. fallback = "ew-resize";
  2705. break;
  2706. case CURSOR_BDIAGSIZE:
  2707. fallback = "fd_double_arrow";
  2708. break;
  2709. case CURSOR_FDIAGSIZE:
  2710. fallback = "bd_double_arrow";
  2711. break;
  2712. case CURSOR_MOVE:
  2713. img[i] = img[CURSOR_DRAG];
  2714. break;
  2715. case CURSOR_VSPLIT:
  2716. fallback = "sb_v_double_arrow";
  2717. break;
  2718. case CURSOR_HSPLIT:
  2719. fallback = "sb_h_double_arrow";
  2720. break;
  2721. case CURSOR_HELP:
  2722. fallback = "help";
  2723. break;
  2724. }
  2725. if (fallback != NULL) {
  2726. img[i] = XcursorLibraryLoadImage(fallback, cursor_theme, cursor_size);
  2727. }
  2728. }
  2729. if (img[i]) {
  2730. cursors[i] = XcursorImageLoadCursor(x11_display, img[i]);
  2731. } else {
  2732. print_verbose("Failed loading custom cursor: " + String(cursor_file[i]));
  2733. }
  2734. }
  2735. {
  2736. // Creating an empty/transparent cursor
  2737. // Create 1x1 bitmap
  2738. Pixmap cursormask = XCreatePixmap(x11_display,
  2739. RootWindow(x11_display, DefaultScreen(x11_display)), 1, 1, 1);
  2740. // Fill with zero
  2741. XGCValues xgc;
  2742. xgc.function = GXclear;
  2743. GC gc = XCreateGC(x11_display, cursormask, GCFunction, &xgc);
  2744. XFillRectangle(x11_display, cursormask, gc, 0, 0, 1, 1);
  2745. // Color value doesn't matter. Mask zero means no foreground or background will be drawn
  2746. XColor col = {};
  2747. Cursor cursor = XCreatePixmapCursor(x11_display,
  2748. cursormask, // source (using cursor mask as placeholder, since it'll all be ignored)
  2749. cursormask, // mask
  2750. &col, &col, 0, 0);
  2751. XFreePixmap(x11_display, cursormask);
  2752. XFreeGC(x11_display, gc);
  2753. if (cursor == None) {
  2754. ERR_PRINT("FAILED CREATING CURSOR");
  2755. }
  2756. null_cursor = cursor;
  2757. }
  2758. cursor_set_shape(CURSOR_BUSY);
  2759. requested = None;
  2760. window_has_focus = true; // Set focus to true at init
  2761. /*if (p_desired.layered) {
  2762. set_window_per_pixel_transparency_enabled(true);
  2763. }*/
  2764. XEvent xevent;
  2765. while (XPending(x11_display) > 0) {
  2766. XNextEvent(x11_display, &xevent);
  2767. if (xevent.type == ConfigureNotify) {
  2768. _window_changed(&xevent);
  2769. }
  2770. }
  2771. _update_real_mouse_position(windows[MAIN_WINDOW_ID]);
  2772. r_error = OK;
  2773. }
  2774. DisplayServerX11::~DisplayServerX11() {
  2775. //destroy all windows
  2776. for (Map<WindowID, WindowData>::Element *E = windows.front(); E; E = E->next()) {
  2777. #ifdef VULKAN_ENABLED
  2778. if (rendering_driver == "vulkan") {
  2779. context_vulkan->window_destroy(E->key());
  2780. }
  2781. #endif
  2782. if (E->get().xic) {
  2783. XDestroyIC(E->get().xic);
  2784. }
  2785. XUnmapWindow(x11_display, E->get().x11_window);
  2786. XDestroyWindow(x11_display, E->get().x11_window);
  2787. }
  2788. //destroy drivers
  2789. #if defined(VULKAN_ENABLED)
  2790. if (rendering_driver == "vulkan") {
  2791. if (rendering_device_vulkan) {
  2792. rendering_device_vulkan->finalize();
  2793. memdelete(rendering_device_vulkan);
  2794. }
  2795. if (context_vulkan)
  2796. memdelete(context_vulkan);
  2797. }
  2798. #endif
  2799. if (xrandr_handle)
  2800. dlclose(xrandr_handle);
  2801. for (int i = 0; i < CURSOR_MAX; i++) {
  2802. if (cursors[i] != None)
  2803. XFreeCursor(x11_display, cursors[i]);
  2804. if (img[i] != NULL)
  2805. XcursorImageDestroy(img[i]);
  2806. };
  2807. if (xim) {
  2808. XCloseIM(xim);
  2809. }
  2810. XCloseDisplay(x11_display);
  2811. if (xmbstring)
  2812. memfree(xmbstring);
  2813. }
  2814. void DisplayServerX11::register_x11_driver() {
  2815. register_create_function("x11", create_func, get_rendering_drivers_func);
  2816. }
  2817. #endif // X11 enabled