os_x11.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712
  1. /*************************************************************************/
  2. /* os_x11.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "os_x11.h"
  31. #include "drivers/gles3/rasterizer_gles3.h"
  32. #include "errno.h"
  33. #include "key_mapping_x11.h"
  34. #include "os/dir_access.h"
  35. #include "print_string.h"
  36. #include "servers/visual/visual_server_raster.h"
  37. #include "servers/visual/visual_server_wrap_mt.h"
  38. #ifdef HAVE_MNTENT
  39. #include <mntent.h>
  40. #endif
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include "X11/Xutil.h"
  45. #include "X11/Xatom.h"
  46. #include "X11/extensions/Xinerama.h"
  47. // ICCCM
  48. #define WM_NormalState 1L // window normal state
  49. #define WM_IconicState 3L // window minimized
  50. // EWMH
  51. #define _NET_WM_STATE_REMOVE 0L // remove/unset property
  52. #define _NET_WM_STATE_ADD 1L // add/set property
  53. #define _NET_WM_STATE_TOGGLE 2L // toggle property
  54. #include "main/main.h"
  55. #include <dlfcn.h>
  56. #include <fcntl.h>
  57. #include <sys/stat.h>
  58. #include <sys/types.h>
  59. #include <unistd.h>
  60. //stupid linux.h
  61. #ifdef KEY_TAB
  62. #undef KEY_TAB
  63. #endif
  64. #include <X11/Xatom.h>
  65. #undef CursorShape
  66. #include <X11/XKBlib.h>
  67. int OS_X11::get_video_driver_count() const {
  68. return 1;
  69. }
  70. const char *OS_X11::get_video_driver_name(int p_driver) const {
  71. return "GLES3";
  72. }
  73. int OS_X11::get_audio_driver_count() const {
  74. return AudioDriverManager::get_driver_count();
  75. }
  76. const char *OS_X11::get_audio_driver_name(int p_driver) const {
  77. AudioDriver *driver = AudioDriverManager::get_driver(p_driver);
  78. ERR_FAIL_COND_V(!driver, "");
  79. return AudioDriverManager::get_driver(p_driver)->get_name();
  80. }
  81. void OS_X11::initialize_core() {
  82. crash_handler.initialize();
  83. OS_Unix::initialize_core();
  84. }
  85. Error OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  86. long im_event_mask = 0;
  87. last_button_state = 0;
  88. xmbstring = NULL;
  89. x11_window = 0;
  90. last_click_ms = 0;
  91. args = OS::get_singleton()->get_cmdline_args();
  92. current_videomode = p_desired;
  93. main_loop = NULL;
  94. last_timestamp = 0;
  95. last_mouse_pos_valid = false;
  96. last_keyrelease_time = 0;
  97. xdnd_version = 0;
  98. if (get_render_thread_mode() == RENDER_SEPARATE_THREAD) {
  99. XInitThreads();
  100. }
  101. /** XLIB INITIALIZATION **/
  102. x11_display = XOpenDisplay(NULL);
  103. if (!x11_display) {
  104. ERR_PRINT("X11 Display is not available");
  105. return ERR_UNAVAILABLE;
  106. }
  107. char *modifiers = NULL;
  108. Bool xkb_dar = False;
  109. XAutoRepeatOn(x11_display);
  110. xkb_dar = XkbSetDetectableAutoRepeat(x11_display, True, NULL);
  111. // Try to support IME if detectable auto-repeat is supported
  112. if (xkb_dar == True) {
  113. #ifdef X_HAVE_UTF8_STRING
  114. // Xutf8LookupString will be used later instead of XmbLookupString before
  115. // the multibyte sequences can be converted to unicode string.
  116. modifiers = XSetLocaleModifiers("");
  117. #endif
  118. }
  119. if (modifiers == NULL) {
  120. if (is_stdout_verbose()) {
  121. WARN_PRINT("IME is disabled");
  122. }
  123. modifiers = XSetLocaleModifiers("@im=none");
  124. WARN_PRINT("Error setting locale modifiers");
  125. }
  126. const char *err;
  127. xrr_get_monitors = NULL;
  128. xrr_free_monitors = NULL;
  129. int xrandr_major = 0;
  130. int xrandr_minor = 0;
  131. int event_base, error_base;
  132. xrandr_ext_ok = XRRQueryExtension(x11_display, &event_base, &error_base);
  133. xrandr_handle = dlopen("libXrandr.so.2", RTLD_LAZY);
  134. if (!xrandr_handle) {
  135. err = dlerror();
  136. fprintf(stderr, "could not load libXrandr.so.2, Error: %s\n", err);
  137. } else {
  138. XRRQueryVersion(x11_display, &xrandr_major, &xrandr_minor);
  139. if (((xrandr_major << 8) | xrandr_minor) >= 0x0105) {
  140. xrr_get_monitors = (xrr_get_monitors_t)dlsym(xrandr_handle, "XRRGetMonitors");
  141. if (!xrr_get_monitors) {
  142. err = dlerror();
  143. fprintf(stderr, "could not find symbol XRRGetMonitors\nError: %s\n", err);
  144. } else {
  145. xrr_free_monitors = (xrr_free_monitors_t)dlsym(xrandr_handle, "XRRFreeMonitors");
  146. if (!xrr_free_monitors) {
  147. err = dlerror();
  148. fprintf(stderr, "could not find XRRFreeMonitors\nError: %s\n", err);
  149. xrr_get_monitors = NULL;
  150. }
  151. }
  152. }
  153. }
  154. #ifdef TOUCH_ENABLED
  155. if (!XQueryExtension(x11_display, "XInputExtension", &touch.opcode, &event_base, &error_base)) {
  156. fprintf(stderr, "XInput extension not available");
  157. } else {
  158. // 2.2 is the first release with multitouch
  159. int xi_major = 2;
  160. int xi_minor = 2;
  161. if (XIQueryVersion(x11_display, &xi_major, &xi_minor) != Success) {
  162. fprintf(stderr, "XInput 2.2 not available (server supports %d.%d)\n", xi_major, xi_minor);
  163. touch.opcode = 0;
  164. } else {
  165. int dev_count;
  166. XIDeviceInfo *info = XIQueryDevice(x11_display, XIAllDevices, &dev_count);
  167. for (int i = 0; i < dev_count; i++) {
  168. XIDeviceInfo *dev = &info[i];
  169. if (!dev->enabled)
  170. continue;
  171. if (!(dev->use == XIMasterPointer || dev->use == XIFloatingSlave))
  172. continue;
  173. bool direct_touch = false;
  174. for (int j = 0; j < dev->num_classes; j++) {
  175. if (dev->classes[j]->type == XITouchClass && ((XITouchClassInfo *)dev->classes[j])->mode == XIDirectTouch) {
  176. direct_touch = true;
  177. break;
  178. }
  179. }
  180. if (direct_touch) {
  181. touch.devices.push_back(dev->deviceid);
  182. fprintf(stderr, "Using touch device: %s\n", dev->name);
  183. }
  184. }
  185. XIFreeDeviceInfo(info);
  186. if (is_stdout_verbose() && !touch.devices.size()) {
  187. fprintf(stderr, "No touch devices found\n");
  188. }
  189. }
  190. }
  191. #endif
  192. xim = XOpenIM(x11_display, NULL, NULL, NULL);
  193. if (xim == NULL) {
  194. WARN_PRINT("XOpenIM failed");
  195. xim_style = 0L;
  196. } else {
  197. ::XIMCallback im_destroy_callback;
  198. im_destroy_callback.client_data = (::XPointer)(this);
  199. im_destroy_callback.callback = (::XIMProc)(xim_destroy_callback);
  200. if (XSetIMValues(xim, XNDestroyCallback, &im_destroy_callback,
  201. NULL) != NULL) {
  202. WARN_PRINT("Error setting XIM destroy callback");
  203. }
  204. ::XIMStyles *xim_styles = NULL;
  205. xim_style = 0L;
  206. char *imvalret = XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
  207. if (imvalret != NULL || xim_styles == NULL) {
  208. fprintf(stderr, "Input method doesn't support any styles\n");
  209. }
  210. if (xim_styles) {
  211. xim_style = 0L;
  212. for (int i = 0; i < xim_styles->count_styles; i++) {
  213. if (xim_styles->supported_styles[i] ==
  214. (XIMPreeditNothing | XIMStatusNothing)) {
  215. xim_style = xim_styles->supported_styles[i];
  216. break;
  217. }
  218. }
  219. XFree(xim_styles);
  220. }
  221. XFree(imvalret);
  222. }
  223. /*
  224. char* windowid = getenv("GODOT_WINDOWID");
  225. if (windowid) {
  226. //freopen("/home/punto/stdout", "w", stdout);
  227. //reopen("/home/punto/stderr", "w", stderr);
  228. x11_window = atol(windowid);
  229. XWindowAttributes xwa;
  230. XGetWindowAttributes(x11_display,x11_window,&xwa);
  231. current_videomode.width = xwa.width;
  232. current_videomode.height = xwa.height;
  233. };
  234. */
  235. // maybe contextgl wants to be in charge of creating the window
  236. //print_line("def videomode "+itos(current_videomode.width)+","+itos(current_videomode.height));
  237. #if defined(OPENGL_ENABLED)
  238. context_gl = memnew(ContextGL_X11(x11_display, x11_window, current_videomode, true));
  239. context_gl->initialize();
  240. RasterizerGLES3::register_config();
  241. RasterizerGLES3::make_current();
  242. context_gl->set_use_vsync(current_videomode.use_vsync);
  243. #endif
  244. visual_server = memnew(VisualServerRaster);
  245. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  246. visual_server = memnew(VisualServerWrapMT(visual_server, get_render_thread_mode() == RENDER_SEPARATE_THREAD));
  247. }
  248. if (current_videomode.maximized) {
  249. current_videomode.maximized = false;
  250. set_window_maximized(true);
  251. // borderless fullscreen window mode
  252. } else if (current_videomode.fullscreen) {
  253. current_videomode.fullscreen = false;
  254. set_window_fullscreen(true);
  255. } else if (current_videomode.borderless_window) {
  256. Hints hints;
  257. Atom property;
  258. hints.flags = 2;
  259. hints.decorations = 0;
  260. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  261. XChangeProperty(x11_display, x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  262. }
  263. // disable resizable window
  264. if (!current_videomode.resizable) {
  265. XSizeHints *xsh;
  266. xsh = XAllocSizeHints();
  267. xsh->flags = PMinSize | PMaxSize;
  268. XWindowAttributes xwa;
  269. if (current_videomode.fullscreen) {
  270. XGetWindowAttributes(x11_display, DefaultRootWindow(x11_display), &xwa);
  271. } else {
  272. XGetWindowAttributes(x11_display, x11_window, &xwa);
  273. }
  274. xsh->min_width = xwa.width;
  275. xsh->max_width = xwa.width;
  276. xsh->min_height = xwa.height;
  277. xsh->max_height = xwa.height;
  278. XSetWMNormalHints(x11_display, x11_window, xsh);
  279. XFree(xsh);
  280. }
  281. if (current_videomode.always_on_top) {
  282. current_videomode.always_on_top = false;
  283. set_window_always_on_top(true);
  284. }
  285. AudioDriverManager::initialize(p_audio_driver);
  286. ERR_FAIL_COND_V(!visual_server, ERR_UNAVAILABLE);
  287. ERR_FAIL_COND_V(x11_window == 0, ERR_UNAVAILABLE);
  288. XSetWindowAttributes new_attr;
  289. new_attr.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
  290. ButtonReleaseMask | EnterWindowMask |
  291. LeaveWindowMask | PointerMotionMask |
  292. Button1MotionMask |
  293. Button2MotionMask | Button3MotionMask |
  294. Button4MotionMask | Button5MotionMask |
  295. ButtonMotionMask | KeymapStateMask |
  296. ExposureMask | VisibilityChangeMask |
  297. StructureNotifyMask |
  298. SubstructureNotifyMask | SubstructureRedirectMask |
  299. FocusChangeMask | PropertyChangeMask |
  300. ColormapChangeMask | OwnerGrabButtonMask |
  301. im_event_mask;
  302. XChangeWindowAttributes(x11_display, x11_window, CWEventMask, &new_attr);
  303. #ifdef TOUCH_ENABLED
  304. if (touch.devices.size()) {
  305. // Must be alive after this block
  306. static unsigned char mask_data[XIMaskLen(XI_LASTEVENT)] = {};
  307. touch.event_mask.deviceid = XIAllDevices;
  308. touch.event_mask.mask_len = sizeof(mask_data);
  309. touch.event_mask.mask = mask_data;
  310. XISetMask(touch.event_mask.mask, XI_TouchBegin);
  311. XISetMask(touch.event_mask.mask, XI_TouchUpdate);
  312. XISetMask(touch.event_mask.mask, XI_TouchEnd);
  313. XISetMask(touch.event_mask.mask, XI_TouchOwnership);
  314. XISelectEvents(x11_display, x11_window, &touch.event_mask, 1);
  315. // Disabled by now since grabbing also blocks mouse events
  316. // (they are received as extended events instead of standard events)
  317. /*XIClearMask(touch.event_mask.mask, XI_TouchOwnership);
  318. // Grab touch devices to avoid OS gesture interference
  319. for (int i = 0; i < touch.devices.size(); ++i) {
  320. XIGrabDevice(x11_display, touch.devices[i], x11_window, CurrentTime, None, XIGrabModeAsync, XIGrabModeAsync, False, &touch.event_mask);
  321. }*/
  322. }
  323. #endif
  324. /* set the titlebar name */
  325. XStoreName(x11_display, x11_window, "Godot");
  326. wm_delete = XInternAtom(x11_display, "WM_DELETE_WINDOW", true);
  327. XSetWMProtocols(x11_display, x11_window, &wm_delete, 1);
  328. if (xim && xim_style) {
  329. xic = XCreateIC(xim, XNInputStyle, xim_style, XNClientWindow, x11_window, XNFocusWindow, x11_window, (char *)NULL);
  330. if (XGetICValues(xic, XNFilterEvents, &im_event_mask, NULL) != NULL) {
  331. WARN_PRINT("XGetICValues couldn't obtain XNFilterEvents value");
  332. XDestroyIC(xic);
  333. xic = NULL;
  334. }
  335. if (xic) {
  336. XSetICFocus(xic);
  337. } else {
  338. WARN_PRINT("XCreateIC couldn't create xic");
  339. }
  340. } else {
  341. xic = NULL;
  342. WARN_PRINT("XCreateIC couldn't create xic");
  343. }
  344. cursor_size = XcursorGetDefaultSize(x11_display);
  345. cursor_theme = XcursorGetTheme(x11_display);
  346. if (!cursor_theme) {
  347. if (is_stdout_verbose()) {
  348. print_line("XcursorGetTheme could not get cursor theme");
  349. }
  350. cursor_theme = "default";
  351. }
  352. for (int i = 0; i < CURSOR_MAX; i++) {
  353. cursors[i] = None;
  354. img[i] = NULL;
  355. }
  356. current_cursor = CURSOR_ARROW;
  357. if (cursor_theme) {
  358. //print_line("cursor theme: "+String(cursor_theme));
  359. for (int i = 0; i < CURSOR_MAX; i++) {
  360. static const char *cursor_file[] = {
  361. "left_ptr",
  362. "xterm",
  363. "hand2",
  364. "cross",
  365. "watch",
  366. "left_ptr_watch",
  367. "fleur",
  368. "hand1",
  369. "X_cursor",
  370. "sb_v_double_arrow",
  371. "sb_h_double_arrow",
  372. "size_bdiag",
  373. "size_fdiag",
  374. "hand1",
  375. "sb_v_double_arrow",
  376. "sb_h_double_arrow",
  377. "question_arrow"
  378. };
  379. img[i] = XcursorLibraryLoadImage(cursor_file[i], cursor_theme, cursor_size);
  380. if (img[i]) {
  381. cursors[i] = XcursorImageLoadCursor(x11_display, img[i]);
  382. //print_line("found cursor: "+String(cursor_file[i])+" id "+itos(cursors[i]));
  383. } else {
  384. if (OS::is_stdout_verbose())
  385. print_line("failed cursor: " + String(cursor_file[i]));
  386. }
  387. }
  388. }
  389. {
  390. Pixmap cursormask;
  391. XGCValues xgc;
  392. GC gc;
  393. XColor col;
  394. Cursor cursor;
  395. cursormask = XCreatePixmap(x11_display, RootWindow(x11_display, DefaultScreen(x11_display)), 1, 1, 1);
  396. xgc.function = GXclear;
  397. gc = XCreateGC(x11_display, cursormask, GCFunction, &xgc);
  398. XFillRectangle(x11_display, cursormask, gc, 0, 0, 1, 1);
  399. col.pixel = 0;
  400. col.red = 0;
  401. col.flags = 4;
  402. cursor = XCreatePixmapCursor(x11_display,
  403. cursormask, cursormask,
  404. &col, &col, 0, 0);
  405. XFreePixmap(x11_display, cursormask);
  406. XFreeGC(x11_display, gc);
  407. if (cursor == None) {
  408. ERR_PRINT("FAILED CREATING CURSOR");
  409. }
  410. null_cursor = cursor;
  411. }
  412. set_cursor_shape(CURSOR_BUSY);
  413. //Set Xdnd (drag & drop) support
  414. Atom XdndAware = XInternAtom(x11_display, "XdndAware", False);
  415. Atom version = 5;
  416. XChangeProperty(x11_display, x11_window, XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char *)&version, 1);
  417. xdnd_enter = XInternAtom(x11_display, "XdndEnter", False);
  418. xdnd_position = XInternAtom(x11_display, "XdndPosition", False);
  419. xdnd_status = XInternAtom(x11_display, "XdndStatus", False);
  420. xdnd_action_copy = XInternAtom(x11_display, "XdndActionCopy", False);
  421. xdnd_drop = XInternAtom(x11_display, "XdndDrop", False);
  422. xdnd_finished = XInternAtom(x11_display, "XdndFinished", False);
  423. xdnd_selection = XInternAtom(x11_display, "XdndSelection", False);
  424. requested = None;
  425. visual_server->init();
  426. input = memnew(InputDefault);
  427. window_has_focus = true; // Set focus to true at init
  428. #ifdef JOYDEV_ENABLED
  429. joypad = memnew(JoypadLinux(input));
  430. #endif
  431. _ensure_user_data_dir();
  432. power_manager = memnew(PowerX11);
  433. XEvent xevent;
  434. while (XPending(x11_display) > 0) {
  435. XNextEvent(x11_display, &xevent);
  436. if (xevent.type == ConfigureNotify) {
  437. _window_changed(&xevent);
  438. }
  439. }
  440. return OK;
  441. }
  442. void OS_X11::xim_destroy_callback(::XIM im, ::XPointer client_data,
  443. ::XPointer call_data) {
  444. WARN_PRINT("Input method stopped");
  445. OS_X11 *os = reinterpret_cast<OS_X11 *>(client_data);
  446. os->xim = NULL;
  447. os->xic = NULL;
  448. }
  449. void OS_X11::set_ime_position(const Point2 &p_pos) {
  450. if (!xic)
  451. return;
  452. ::XPoint spot;
  453. spot.x = short(p_pos.x);
  454. spot.y = short(p_pos.y);
  455. XVaNestedList preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL);
  456. XSetICValues(xic, XNPreeditAttributes, preedit_attr, NULL);
  457. XFree(preedit_attr);
  458. }
  459. void OS_X11::finalize() {
  460. if (main_loop)
  461. memdelete(main_loop);
  462. main_loop = NULL;
  463. /*
  464. if (debugger_connection_console) {
  465. memdelete(debugger_connection_console);
  466. }
  467. */
  468. #ifdef JOYDEV_ENABLED
  469. memdelete(joypad);
  470. #endif
  471. #ifdef TOUCH_ENABLED
  472. touch.devices.clear();
  473. touch.state.clear();
  474. #endif
  475. memdelete(input);
  476. visual_server->finish();
  477. memdelete(visual_server);
  478. //memdelete(rasterizer);
  479. memdelete(power_manager);
  480. if (xrandr_handle)
  481. dlclose(xrandr_handle);
  482. XUnmapWindow(x11_display, x11_window);
  483. XDestroyWindow(x11_display, x11_window);
  484. #if defined(OPENGL_ENABLED)
  485. memdelete(context_gl);
  486. #endif
  487. for (int i = 0; i < CURSOR_MAX; i++) {
  488. if (cursors[i] != None)
  489. XFreeCursor(x11_display, cursors[i]);
  490. if (img[i] != NULL)
  491. XcursorImageDestroy(img[i]);
  492. };
  493. if (xic) {
  494. XDestroyIC(xic);
  495. }
  496. if (xim) {
  497. XCloseIM(xim);
  498. }
  499. XCloseDisplay(x11_display);
  500. if (xmbstring)
  501. memfree(xmbstring);
  502. args.clear();
  503. }
  504. void OS_X11::set_mouse_mode(MouseMode p_mode) {
  505. if (p_mode == mouse_mode)
  506. return;
  507. if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED)
  508. XUngrabPointer(x11_display, CurrentTime);
  509. // The only modes that show a cursor are VISIBLE and CONFINED
  510. bool showCursor = (p_mode == MOUSE_MODE_VISIBLE || p_mode == MOUSE_MODE_CONFINED);
  511. if (showCursor) {
  512. XUndefineCursor(x11_display, x11_window); // show cursor
  513. } else {
  514. XDefineCursor(x11_display, x11_window, null_cursor); // hide cursor
  515. }
  516. mouse_mode = p_mode;
  517. if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED) {
  518. while (true) {
  519. //flush pending motion events
  520. if (XPending(x11_display) > 0) {
  521. XEvent event;
  522. XPeekEvent(x11_display, &event);
  523. if (event.type == MotionNotify) {
  524. XNextEvent(x11_display, &event);
  525. } else {
  526. break;
  527. }
  528. } else {
  529. break;
  530. }
  531. }
  532. if (XGrabPointer(
  533. x11_display, x11_window, True,
  534. ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
  535. GrabModeAsync, GrabModeAsync, x11_window, None, CurrentTime) != GrabSuccess) {
  536. ERR_PRINT("NO GRAB");
  537. }
  538. center.x = current_videomode.width / 2;
  539. center.y = current_videomode.height / 2;
  540. XWarpPointer(x11_display, None, x11_window,
  541. 0, 0, 0, 0, (int)center.x, (int)center.y);
  542. input->set_mouse_position(center);
  543. } else {
  544. do_mouse_warp = false;
  545. }
  546. XFlush(x11_display);
  547. }
  548. void OS_X11::warp_mouse_position(const Point2 &p_to) {
  549. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  550. last_mouse_pos = p_to;
  551. } else {
  552. /*XWindowAttributes xwa;
  553. XGetWindowAttributes(x11_display, x11_window, &xwa);
  554. printf("%d %d\n", xwa.x, xwa.y); needed? */
  555. XWarpPointer(x11_display, None, x11_window,
  556. 0, 0, 0, 0, (int)p_to.x, (int)p_to.y);
  557. }
  558. }
  559. OS::MouseMode OS_X11::get_mouse_mode() const {
  560. return mouse_mode;
  561. }
  562. int OS_X11::get_mouse_button_state() const {
  563. return last_button_state;
  564. }
  565. Point2 OS_X11::get_mouse_position() const {
  566. return last_mouse_pos;
  567. }
  568. void OS_X11::set_window_title(const String &p_title) {
  569. XStoreName(x11_display, x11_window, p_title.utf8().get_data());
  570. Atom _net_wm_name = XInternAtom(x11_display, "_NET_WM_NAME", false);
  571. Atom utf8_string = XInternAtom(x11_display, "UTF8_STRING", false);
  572. XChangeProperty(x11_display, x11_window, _net_wm_name, utf8_string, 8, PropModeReplace, (unsigned char *)p_title.utf8().get_data(), p_title.utf8().length());
  573. }
  574. void OS_X11::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  575. }
  576. OS::VideoMode OS_X11::get_video_mode(int p_screen) const {
  577. return current_videomode;
  578. }
  579. void OS_X11::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  580. }
  581. void OS_X11::set_wm_fullscreen(bool p_enabled) {
  582. if (p_enabled && !get_borderless_window()) {
  583. // remove decorations if the window is not already borderless
  584. Hints hints;
  585. Atom property;
  586. hints.flags = 2;
  587. hints.decorations = 0;
  588. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  589. XChangeProperty(x11_display, x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  590. }
  591. if (p_enabled && !is_window_resizable()) {
  592. // Set the window as resizable to prevent window managers to ignore the fullscreen state flag.
  593. XSizeHints *xsh;
  594. xsh = XAllocSizeHints();
  595. xsh->flags = 0L;
  596. XSetWMNormalHints(x11_display, x11_window, xsh);
  597. XFree(xsh);
  598. }
  599. // Using EWMH -- Extended Window Manager Hints
  600. XEvent xev;
  601. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  602. Atom wm_fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False);
  603. memset(&xev, 0, sizeof(xev));
  604. xev.type = ClientMessage;
  605. xev.xclient.window = x11_window;
  606. xev.xclient.message_type = wm_state;
  607. xev.xclient.format = 32;
  608. xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  609. xev.xclient.data.l[1] = wm_fullscreen;
  610. xev.xclient.data.l[2] = 0;
  611. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  612. // set bypass compositor hint
  613. Atom bypass_compositor = XInternAtom(x11_display, "_NET_WM_BYPASS_COMPOSITOR", False);
  614. unsigned long compositing_disable_on = p_enabled ? 1 : 0;
  615. XChangeProperty(x11_display, x11_window, bypass_compositor, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&compositing_disable_on, 1);
  616. XFlush(x11_display);
  617. if (!p_enabled && !is_window_resizable()) {
  618. // Reset the non-resizable flags if we un-set these before.
  619. Size2 size = get_window_size();
  620. XSizeHints *xsh;
  621. xsh = XAllocSizeHints();
  622. xsh->flags = PMinSize | PMaxSize;
  623. xsh->min_width = size.x;
  624. xsh->max_width = size.x;
  625. xsh->min_height = size.y;
  626. xsh->max_height = size.y;
  627. XSetWMNormalHints(x11_display, x11_window, xsh);
  628. XFree(xsh);
  629. }
  630. if (!p_enabled && !get_borderless_window()) {
  631. // put decorations back if the window wasn't suppoesed to be borderless
  632. Hints hints;
  633. Atom property;
  634. hints.flags = 2;
  635. hints.decorations = 1;
  636. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  637. XChangeProperty(x11_display, x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  638. }
  639. }
  640. void OS_X11::set_wm_above(bool p_enabled) {
  641. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  642. Atom wm_above = XInternAtom(x11_display, "_NET_WM_STATE_ABOVE", False);
  643. XClientMessageEvent xev;
  644. memset(&xev, 0, sizeof(xev));
  645. xev.type = ClientMessage;
  646. xev.window = x11_window;
  647. xev.message_type = wm_state;
  648. xev.format = 32;
  649. xev.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  650. xev.data.l[1] = wm_above;
  651. xev.data.l[3] = 1;
  652. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xev);
  653. }
  654. int OS_X11::get_screen_count() const {
  655. // Using Xinerama Extension
  656. int event_base, error_base;
  657. const Bool ext_okay = XineramaQueryExtension(x11_display, &event_base, &error_base);
  658. if (!ext_okay) return 0;
  659. int count;
  660. XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
  661. XFree(xsi);
  662. return count;
  663. }
  664. int OS_X11::get_current_screen() const {
  665. int x, y;
  666. Window child;
  667. XTranslateCoordinates(x11_display, x11_window, DefaultRootWindow(x11_display), 0, 0, &x, &y, &child);
  668. int count = get_screen_count();
  669. for (int i = 0; i < count; i++) {
  670. Point2i pos = get_screen_position(i);
  671. Size2i size = get_screen_size(i);
  672. if ((x >= pos.x && x < pos.x + size.width) && (y >= pos.y && y < pos.y + size.height))
  673. return i;
  674. }
  675. return 0;
  676. }
  677. void OS_X11::set_current_screen(int p_screen) {
  678. int count = get_screen_count();
  679. if (p_screen >= count) return;
  680. if (current_videomode.fullscreen) {
  681. Point2i position = get_screen_position(p_screen);
  682. Size2i size = get_screen_size(p_screen);
  683. XMoveResizeWindow(x11_display, x11_window, position.x, position.y, size.x, size.y);
  684. } else {
  685. if (p_screen != get_current_screen()) {
  686. Point2i position = get_screen_position(p_screen);
  687. XMoveWindow(x11_display, x11_window, position.x, position.y);
  688. }
  689. }
  690. }
  691. Point2 OS_X11::get_screen_position(int p_screen) const {
  692. if (p_screen == -1) {
  693. p_screen = get_current_screen();
  694. }
  695. // Using Xinerama Extension
  696. int event_base, error_base;
  697. const Bool ext_okay = XineramaQueryExtension(x11_display, &event_base, &error_base);
  698. if (!ext_okay) {
  699. return Point2i(0, 0);
  700. }
  701. int count;
  702. XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
  703. if (p_screen >= count) {
  704. return Point2i(0, 0);
  705. }
  706. Point2i position = Point2i(xsi[p_screen].x_org, xsi[p_screen].y_org);
  707. XFree(xsi);
  708. return position;
  709. }
  710. Size2 OS_X11::get_screen_size(int p_screen) const {
  711. if (p_screen == -1) {
  712. p_screen = get_current_screen();
  713. }
  714. // Using Xinerama Extension
  715. int event_base, error_base;
  716. const Bool ext_okay = XineramaQueryExtension(x11_display, &event_base, &error_base);
  717. if (!ext_okay) return Size2i(0, 0);
  718. int count;
  719. XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
  720. if (p_screen >= count) return Size2i(0, 0);
  721. Size2i size = Point2i(xsi[p_screen].width, xsi[p_screen].height);
  722. XFree(xsi);
  723. return size;
  724. }
  725. int OS_X11::get_screen_dpi(int p_screen) const {
  726. if (p_screen == -1) {
  727. p_screen = get_current_screen();
  728. }
  729. //invalid screen?
  730. ERR_FAIL_INDEX_V(p_screen, get_screen_count(), 0);
  731. //Get physical monitor Dimensions through XRandR and calculate dpi
  732. Size2 sc = get_screen_size(p_screen);
  733. if (xrandr_ext_ok) {
  734. int count = 0;
  735. if (xrr_get_monitors) {
  736. xrr_monitor_info *monitors = xrr_get_monitors(x11_display, x11_window, true, &count);
  737. if (p_screen < count) {
  738. double xdpi = sc.width / (double)monitors[p_screen].mwidth * 25.4;
  739. double ydpi = sc.height / (double)monitors[p_screen].mheight * 25.4;
  740. xrr_free_monitors(monitors);
  741. return (xdpi + ydpi) / 2;
  742. }
  743. xrr_free_monitors(monitors);
  744. } else if (p_screen == 0) {
  745. XRRScreenSize *sizes = XRRSizes(x11_display, 0, &count);
  746. if (sizes) {
  747. double xdpi = sc.width / (double)sizes[0].mwidth * 25.4;
  748. double ydpi = sc.height / (double)sizes[0].mheight * 25.4;
  749. return (xdpi + ydpi) / 2;
  750. }
  751. }
  752. }
  753. int width_mm = DisplayWidthMM(x11_display, p_screen);
  754. int height_mm = DisplayHeightMM(x11_display, p_screen);
  755. double xdpi = (width_mm ? sc.width / (double)width_mm * 25.4 : 0);
  756. double ydpi = (height_mm ? sc.height / (double)height_mm * 25.4 : 0);
  757. if (xdpi || xdpi)
  758. return (xdpi + ydpi) / (xdpi && ydpi ? 2 : 1);
  759. //could not get dpi
  760. return 96;
  761. }
  762. Point2 OS_X11::get_window_position() const {
  763. int x, y;
  764. Window child;
  765. XTranslateCoordinates(x11_display, x11_window, DefaultRootWindow(x11_display), 0, 0, &x, &y, &child);
  766. int screen = get_current_screen();
  767. Point2i screen_position = get_screen_position(screen);
  768. return Point2i(x - screen_position.x, y - screen_position.y);
  769. }
  770. void OS_X11::set_window_position(const Point2 &p_position) {
  771. XMoveWindow(x11_display, x11_window, p_position.x, p_position.y);
  772. }
  773. Size2 OS_X11::get_window_size() const {
  774. // Use current_videomode width and height instead of XGetWindowAttributes
  775. // since right after a XResizeWindow the attributes may not be updated yet
  776. return Size2i(current_videomode.width, current_videomode.height);
  777. }
  778. Size2 OS_X11::get_real_window_size() const {
  779. XWindowAttributes xwa;
  780. XSync(x11_display, False);
  781. XGetWindowAttributes(x11_display, x11_window, &xwa);
  782. int w = xwa.width;
  783. int h = xwa.height;
  784. Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True);
  785. Atom type;
  786. int format;
  787. unsigned long len;
  788. unsigned long remaining;
  789. unsigned char *data = NULL;
  790. if (XGetWindowProperty(x11_display, x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) {
  791. long *extents = (long *)data;
  792. w += extents[0] + extents[1]; // left, right
  793. h += extents[2] + extents[3]; // top, bottom
  794. }
  795. return Size2(w, h);
  796. }
  797. void OS_X11::set_window_size(const Size2 p_size) {
  798. // If window resizable is disabled we need to update the attributes first
  799. if (is_window_resizable() == false) {
  800. XSizeHints *xsh;
  801. xsh = XAllocSizeHints();
  802. xsh->flags = PMinSize | PMaxSize;
  803. xsh->min_width = p_size.x;
  804. xsh->max_width = p_size.x;
  805. xsh->min_height = p_size.y;
  806. xsh->max_height = p_size.y;
  807. XSetWMNormalHints(x11_display, x11_window, xsh);
  808. XFree(xsh);
  809. }
  810. // Resize the window
  811. XResizeWindow(x11_display, x11_window, p_size.x, p_size.y);
  812. // Update our videomode width and height
  813. current_videomode.width = p_size.x;
  814. current_videomode.height = p_size.y;
  815. }
  816. void OS_X11::set_window_fullscreen(bool p_enabled) {
  817. if (current_videomode.fullscreen == p_enabled)
  818. return;
  819. if (p_enabled && current_videomode.always_on_top) {
  820. // Fullscreen + Always-on-top requires a maximized window on some window managers (Metacity)
  821. set_window_maximized(true);
  822. }
  823. set_wm_fullscreen(p_enabled);
  824. if (!p_enabled && !current_videomode.always_on_top) {
  825. // Restore
  826. set_window_maximized(false);
  827. }
  828. current_videomode.fullscreen = p_enabled;
  829. }
  830. bool OS_X11::is_window_fullscreen() const {
  831. return current_videomode.fullscreen;
  832. }
  833. void OS_X11::set_window_resizable(bool p_enabled) {
  834. XSizeHints *xsh;
  835. Size2 size = get_window_size();
  836. xsh = XAllocSizeHints();
  837. xsh->flags = p_enabled ? 0L : PMinSize | PMaxSize;
  838. if (!p_enabled) {
  839. xsh->min_width = size.x;
  840. xsh->max_width = size.x;
  841. xsh->min_height = size.y;
  842. xsh->max_height = size.y;
  843. }
  844. XSetWMNormalHints(x11_display, x11_window, xsh);
  845. XFree(xsh);
  846. current_videomode.resizable = p_enabled;
  847. }
  848. bool OS_X11::is_window_resizable() const {
  849. return current_videomode.resizable;
  850. }
  851. void OS_X11::set_window_minimized(bool p_enabled) {
  852. // Using ICCCM -- Inter-Client Communication Conventions Manual
  853. XEvent xev;
  854. Atom wm_change = XInternAtom(x11_display, "WM_CHANGE_STATE", False);
  855. memset(&xev, 0, sizeof(xev));
  856. xev.type = ClientMessage;
  857. xev.xclient.window = x11_window;
  858. xev.xclient.message_type = wm_change;
  859. xev.xclient.format = 32;
  860. xev.xclient.data.l[0] = p_enabled ? WM_IconicState : WM_NormalState;
  861. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  862. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  863. Atom wm_hidden = XInternAtom(x11_display, "_NET_WM_STATE_HIDDEN", False);
  864. memset(&xev, 0, sizeof(xev));
  865. xev.type = ClientMessage;
  866. xev.xclient.window = x11_window;
  867. xev.xclient.message_type = wm_state;
  868. xev.xclient.format = 32;
  869. xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
  870. xev.xclient.data.l[1] = wm_hidden;
  871. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  872. }
  873. bool OS_X11::is_window_minimized() const {
  874. // Using ICCCM -- Inter-Client Communication Conventions Manual
  875. Atom property = XInternAtom(x11_display, "WM_STATE", True);
  876. Atom type;
  877. int format;
  878. unsigned long len;
  879. unsigned long remaining;
  880. unsigned char *data = NULL;
  881. int result = XGetWindowProperty(
  882. x11_display,
  883. x11_window,
  884. property,
  885. 0,
  886. 32,
  887. False,
  888. AnyPropertyType,
  889. &type,
  890. &format,
  891. &len,
  892. &remaining,
  893. &data);
  894. if (result == Success) {
  895. long *state = (long *)data;
  896. if (state[0] == WM_IconicState)
  897. return true;
  898. }
  899. return false;
  900. }
  901. void OS_X11::set_window_maximized(bool p_enabled) {
  902. if (is_window_maximized() == p_enabled)
  903. return;
  904. // Using EWMH -- Extended Window Manager Hints
  905. XEvent xev;
  906. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  907. Atom wm_max_horz = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  908. Atom wm_max_vert = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  909. memset(&xev, 0, sizeof(xev));
  910. xev.type = ClientMessage;
  911. xev.xclient.window = x11_window;
  912. xev.xclient.message_type = wm_state;
  913. xev.xclient.format = 32;
  914. xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  915. xev.xclient.data.l[1] = wm_max_horz;
  916. xev.xclient.data.l[2] = wm_max_vert;
  917. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  918. if (is_window_maximize_allowed()) {
  919. while (p_enabled && !is_window_maximized()) {
  920. // Wait for effective resizing (so the GLX context is too).
  921. }
  922. }
  923. maximized = p_enabled;
  924. }
  925. bool OS_X11::is_window_maximize_allowed() {
  926. Atom property = XInternAtom(x11_display, "_NET_WM_ALLOWED_ACTIONS", False);
  927. Atom type;
  928. int format;
  929. unsigned long len;
  930. unsigned long remaining;
  931. unsigned char *data = NULL;
  932. int result = XGetWindowProperty(
  933. x11_display,
  934. x11_window,
  935. property,
  936. 0,
  937. 1024,
  938. False,
  939. XA_ATOM,
  940. &type,
  941. &format,
  942. &len,
  943. &remaining,
  944. &data);
  945. if (result == Success) {
  946. Atom *atoms = (Atom *)data;
  947. Atom wm_act_max_horz = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_HORZ", False);
  948. Atom wm_act_max_vert = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_VERT", False);
  949. bool found_wm_act_max_horz = false;
  950. bool found_wm_act_max_vert = false;
  951. for (unsigned int i = 0; i < len; i++) {
  952. if (atoms[i] == wm_act_max_horz)
  953. found_wm_act_max_horz = true;
  954. if (atoms[i] == wm_act_max_vert)
  955. found_wm_act_max_vert = true;
  956. if (found_wm_act_max_horz || found_wm_act_max_vert)
  957. return true;
  958. }
  959. XFree(atoms);
  960. }
  961. return false;
  962. }
  963. bool OS_X11::is_window_maximized() const {
  964. // Using EWMH -- Extended Window Manager Hints
  965. Atom property = XInternAtom(x11_display, "_NET_WM_STATE", False);
  966. Atom type;
  967. int format;
  968. unsigned long len;
  969. unsigned long remaining;
  970. unsigned char *data = NULL;
  971. bool retval = false;
  972. int result = XGetWindowProperty(
  973. x11_display,
  974. x11_window,
  975. property,
  976. 0,
  977. 1024,
  978. False,
  979. XA_ATOM,
  980. &type,
  981. &format,
  982. &len,
  983. &remaining,
  984. &data);
  985. if (result == Success) {
  986. Atom *atoms = (Atom *)data;
  987. Atom wm_max_horz = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  988. Atom wm_max_vert = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  989. bool found_wm_max_horz = false;
  990. bool found_wm_max_vert = false;
  991. for (unsigned int i = 0; i < len; i++) {
  992. if (atoms[i] == wm_max_horz)
  993. found_wm_max_horz = true;
  994. if (atoms[i] == wm_max_vert)
  995. found_wm_max_vert = true;
  996. if (found_wm_max_horz && found_wm_max_vert) {
  997. retval = true;
  998. break;
  999. }
  1000. }
  1001. }
  1002. XFree(data);
  1003. return retval;
  1004. }
  1005. void OS_X11::set_window_always_on_top(bool p_enabled) {
  1006. if (is_window_always_on_top() == p_enabled)
  1007. return;
  1008. if (p_enabled && current_videomode.fullscreen) {
  1009. // Fullscreen + Always-on-top requires a maximized window on some window managers (Metacity)
  1010. set_window_maximized(true);
  1011. }
  1012. set_wm_above(p_enabled);
  1013. if (!p_enabled && !current_videomode.fullscreen) {
  1014. // Restore
  1015. set_window_maximized(false);
  1016. }
  1017. current_videomode.always_on_top = p_enabled;
  1018. }
  1019. bool OS_X11::is_window_always_on_top() const {
  1020. return current_videomode.always_on_top;
  1021. }
  1022. void OS_X11::set_borderless_window(bool p_borderless) {
  1023. if (current_videomode.borderless_window == p_borderless)
  1024. return;
  1025. current_videomode.borderless_window = p_borderless;
  1026. Hints hints;
  1027. Atom property;
  1028. hints.flags = 2;
  1029. hints.decorations = current_videomode.borderless_window ? 0 : 1;
  1030. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  1031. XChangeProperty(x11_display, x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  1032. }
  1033. bool OS_X11::get_borderless_window() {
  1034. return current_videomode.borderless_window;
  1035. }
  1036. void OS_X11::request_attention() {
  1037. // Using EWMH -- Extended Window Manager Hints
  1038. //
  1039. // Sets the _NET_WM_STATE_DEMANDS_ATTENTION atom for WM_STATE
  1040. // Will be unset by the window manager after user react on the request for attention
  1041. //
  1042. XEvent xev;
  1043. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  1044. Atom wm_attention = XInternAtom(x11_display, "_NET_WM_STATE_DEMANDS_ATTENTION", False);
  1045. memset(&xev, 0, sizeof(xev));
  1046. xev.type = ClientMessage;
  1047. xev.xclient.window = x11_window;
  1048. xev.xclient.message_type = wm_state;
  1049. xev.xclient.format = 32;
  1050. xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
  1051. xev.xclient.data.l[1] = wm_attention;
  1052. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  1053. }
  1054. void OS_X11::get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state) {
  1055. state->set_shift((p_x11_state & ShiftMask));
  1056. state->set_control((p_x11_state & ControlMask));
  1057. state->set_alt((p_x11_state & Mod1Mask /*|| p_x11_state&Mod5Mask*/)); //altgr should not count as alt
  1058. state->set_metakey((p_x11_state & Mod4Mask));
  1059. }
  1060. unsigned int OS_X11::get_mouse_button_state(unsigned int p_x11_state) {
  1061. unsigned int state = 0;
  1062. if (p_x11_state & Button1Mask) {
  1063. state |= 1 << 0;
  1064. }
  1065. if (p_x11_state & Button3Mask) {
  1066. state |= 1 << 1;
  1067. }
  1068. if (p_x11_state & Button2Mask) {
  1069. state |= 1 << 2;
  1070. }
  1071. if (p_x11_state & Button4Mask) {
  1072. state |= 1 << 3;
  1073. }
  1074. if (p_x11_state & Button5Mask) {
  1075. state |= 1 << 4;
  1076. }
  1077. last_button_state = state;
  1078. return state;
  1079. }
  1080. void OS_X11::handle_key_event(XKeyEvent *p_event, bool p_echo) {
  1081. // X11 functions don't know what const is
  1082. XKeyEvent *xkeyevent = p_event;
  1083. // This code was pretty difficult to write.
  1084. // The docs stink and every toolkit seems to
  1085. // do it in a different way.
  1086. /* Phase 1, obtain a proper keysym */
  1087. // This was also very difficult to figure out.
  1088. // You'd expect you could just use Keysym provided by
  1089. // XKeycodeToKeysym to obtain internationalized
  1090. // input.. WRONG!!
  1091. // you must use XLookupString (???) which not only wastes
  1092. // cycles generating an unnecessary string, but also
  1093. // still works in half the cases. (won't handle deadkeys)
  1094. // For more complex input methods (deadkeys and more advanced)
  1095. // you have to use XmbLookupString (??).
  1096. // So.. then you have to chosse which of both results
  1097. // you want to keep.
  1098. // This is a real bizarreness and cpu waster.
  1099. KeySym keysym_keycode = 0; // keysym used to find a keycode
  1100. KeySym keysym_unicode = 0; // keysym used to find unicode
  1101. // XLookupString returns keysyms usable as nice scancodes/
  1102. char str[256 + 1];
  1103. XLookupString(xkeyevent, str, 256, &keysym_keycode, NULL);
  1104. // Meanwhile, XLookupString returns keysyms useful for unicode.
  1105. if (!xmbstring) {
  1106. // keep a temporary buffer for the string
  1107. xmbstring = (char *)memalloc(sizeof(char) * 8);
  1108. xmblen = 8;
  1109. }
  1110. keysym_unicode = keysym_keycode;
  1111. if (xkeyevent->type == KeyPress && xic) {
  1112. Status status;
  1113. #ifdef X_HAVE_UTF8_STRING
  1114. int utf8len = 8;
  1115. char *utf8string = (char *)memalloc(sizeof(char) * utf8len);
  1116. int utf8bytes = Xutf8LookupString(xic, xkeyevent, utf8string,
  1117. utf8len - 1, &keysym_unicode, &status);
  1118. if (status == XBufferOverflow) {
  1119. utf8len = utf8bytes + 1;
  1120. utf8string = (char *)memrealloc(utf8string, utf8len);
  1121. utf8bytes = Xutf8LookupString(xic, xkeyevent, utf8string,
  1122. utf8len - 1, &keysym_unicode, &status);
  1123. }
  1124. utf8string[utf8bytes] = '\0';
  1125. if (status == XLookupChars) {
  1126. bool keypress = xkeyevent->type == KeyPress;
  1127. unsigned int keycode = KeyMappingX11::get_keycode(keysym_keycode);
  1128. if (keycode >= 'a' && keycode <= 'z')
  1129. keycode -= 'a' - 'A';
  1130. String tmp;
  1131. tmp.parse_utf8(utf8string, utf8bytes);
  1132. for (int i = 0; i < tmp.length(); i++) {
  1133. Ref<InputEventKey> k;
  1134. k.instance();
  1135. if (keycode == 0 && tmp[i] == 0) {
  1136. continue;
  1137. }
  1138. get_key_modifier_state(xkeyevent->state, k);
  1139. k->set_unicode(tmp[i]);
  1140. k->set_pressed(keypress);
  1141. k->set_scancode(keycode);
  1142. k->set_echo(false);
  1143. if (k->get_scancode() == KEY_BACKTAB) {
  1144. //make it consistent across platforms.
  1145. k->set_scancode(KEY_TAB);
  1146. k->set_shift(true);
  1147. }
  1148. input->parse_input_event(k);
  1149. }
  1150. return;
  1151. }
  1152. memfree(utf8string);
  1153. #else
  1154. do {
  1155. int mnbytes = XmbLookupString(xic, xkeyevent, xmbstring, xmblen - 1, &keysym_unicode, &status);
  1156. xmbstring[mnbytes] = '\0';
  1157. if (status == XBufferOverflow) {
  1158. xmblen = mnbytes + 1;
  1159. xmbstring = (char *)memrealloc(xmbstring, xmblen);
  1160. }
  1161. } while (status == XBufferOverflow);
  1162. #endif
  1163. }
  1164. /* Phase 2, obtain a pigui keycode from the keysym */
  1165. // KeyMappingX11 just translated the X11 keysym to a PIGUI
  1166. // keysym, so it works in all platforms the same.
  1167. unsigned int keycode = KeyMappingX11::get_keycode(keysym_keycode);
  1168. /* Phase 3, obtain a unicode character from the keysym */
  1169. // KeyMappingX11 also translates keysym to unicode.
  1170. // It does a binary search on a table to translate
  1171. // most properly.
  1172. //print_line("keysym_unicode: "+rtos(keysym_unicode));
  1173. unsigned int unicode = keysym_unicode > 0 ? KeyMappingX11::get_unicode_from_keysym(keysym_unicode) : 0;
  1174. /* Phase 4, determine if event must be filtered */
  1175. // This seems to be a side-effect of using XIM.
  1176. // XEventFilter looks like a core X11 function,
  1177. // but it's actually just used to see if we must
  1178. // ignore a deadkey, or events XIM determines
  1179. // must not reach the actual gui.
  1180. // Guess it was a design problem of the extension
  1181. bool keypress = xkeyevent->type == KeyPress;
  1182. if (keycode == 0 && unicode == 0)
  1183. return;
  1184. /* Phase 5, determine modifier mask */
  1185. // No problems here, except I had no way to
  1186. // know Mod1 was ALT and Mod4 was META (applekey/winkey)
  1187. // just tried Mods until i found them.
  1188. //print_line("mod1: "+itos(xkeyevent->state&Mod1Mask)+" mod 5: "+itos(xkeyevent->state&Mod5Mask));
  1189. Ref<InputEventKey> k;
  1190. k.instance();
  1191. get_key_modifier_state(xkeyevent->state, k);
  1192. /* Phase 6, determine echo character */
  1193. // Echo characters in X11 are a keyrelease and a keypress
  1194. // one after the other with the (almot) same timestamp.
  1195. // To detect them, i use XPeekEvent and check that their
  1196. // difference in time is below a threshold.
  1197. if (xkeyevent->type != KeyPress) {
  1198. p_echo = false;
  1199. // make sure there are events pending,
  1200. // so this call won't block.
  1201. if (XPending(x11_display) > 0) {
  1202. XEvent peek_event;
  1203. XPeekEvent(x11_display, &peek_event);
  1204. // I'm using a threshold of 5 msecs,
  1205. // since sometimes there seems to be a little
  1206. // jitter. I'm still not convinced that all this approach
  1207. // is correct, but the xorg developers are
  1208. // not very helpful today.
  1209. ::Time tresh = ABS(peek_event.xkey.time - xkeyevent->time);
  1210. if (peek_event.type == KeyPress && tresh < 5) {
  1211. KeySym rk;
  1212. XLookupString((XKeyEvent *)&peek_event, str, 256, &rk, NULL);
  1213. if (rk == keysym_keycode) {
  1214. XEvent event;
  1215. XNextEvent(x11_display, &event); //erase next event
  1216. handle_key_event((XKeyEvent *)&event, true);
  1217. return; //ignore current, echo next
  1218. }
  1219. }
  1220. // use the time from peek_event so it always works
  1221. }
  1222. // save the time to check for echo when keypress happens
  1223. }
  1224. /* Phase 7, send event to Window */
  1225. k->set_pressed(keypress);
  1226. if (keycode >= 'a' && keycode <= 'z')
  1227. keycode -= 'a' - 'A';
  1228. k->set_scancode(keycode);
  1229. k->set_unicode(unicode);
  1230. k->set_echo(p_echo);
  1231. if (k->get_scancode() == KEY_BACKTAB) {
  1232. //make it consistent across platforms.
  1233. k->set_scancode(KEY_TAB);
  1234. k->set_shift(true);
  1235. }
  1236. //don't set mod state if modifier keys are released by themselves
  1237. //else event.is_action() will not work correctly here
  1238. if (!k->is_pressed()) {
  1239. if (k->get_scancode() == KEY_SHIFT)
  1240. k->set_shift(false);
  1241. else if (k->get_scancode() == KEY_CONTROL)
  1242. k->set_control(false);
  1243. else if (k->get_scancode() == KEY_ALT)
  1244. k->set_alt(false);
  1245. else if (k->get_scancode() == KEY_META)
  1246. k->set_metakey(false);
  1247. }
  1248. bool last_is_pressed = Input::get_singleton()->is_key_pressed(k->get_scancode());
  1249. if (k->is_pressed()) {
  1250. if (last_is_pressed) {
  1251. k->set_echo(true);
  1252. }
  1253. } else {
  1254. //ignore
  1255. if (last_is_pressed == false) {
  1256. return;
  1257. }
  1258. }
  1259. //printf("key: %x\n",k->get_scancode());
  1260. input->parse_input_event(k);
  1261. }
  1262. struct Property {
  1263. unsigned char *data;
  1264. int format, nitems;
  1265. Atom type;
  1266. };
  1267. static Property read_property(Display *p_display, Window p_window, Atom p_property) {
  1268. Atom actual_type;
  1269. int actual_format;
  1270. unsigned long nitems;
  1271. unsigned long bytes_after;
  1272. unsigned char *ret = 0;
  1273. int read_bytes = 1024;
  1274. //Keep trying to read the property until there are no
  1275. //bytes unread.
  1276. do {
  1277. if (ret != 0)
  1278. XFree(ret);
  1279. XGetWindowProperty(p_display, p_window, p_property, 0, read_bytes, False, AnyPropertyType,
  1280. &actual_type, &actual_format, &nitems, &bytes_after,
  1281. &ret);
  1282. read_bytes *= 2;
  1283. } while (bytes_after != 0);
  1284. Property p = { ret, actual_format, (int)nitems, actual_type };
  1285. return p;
  1286. }
  1287. static Atom pick_target_from_list(Display *p_display, Atom *p_list, int p_count) {
  1288. static const char *target_type = "text/uri-list";
  1289. for (int i = 0; i < p_count; i++) {
  1290. Atom atom = p_list[i];
  1291. if (atom != None && String(XGetAtomName(p_display, atom)) == target_type)
  1292. return atom;
  1293. }
  1294. return None;
  1295. }
  1296. static Atom pick_target_from_atoms(Display *p_disp, Atom p_t1, Atom p_t2, Atom p_t3) {
  1297. static const char *target_type = "text/uri-list";
  1298. if (p_t1 != None && String(XGetAtomName(p_disp, p_t1)) == target_type)
  1299. return p_t1;
  1300. if (p_t2 != None && String(XGetAtomName(p_disp, p_t2)) == target_type)
  1301. return p_t2;
  1302. if (p_t3 != None && String(XGetAtomName(p_disp, p_t3)) == target_type)
  1303. return p_t3;
  1304. return None;
  1305. }
  1306. void OS_X11::_window_changed(XEvent *event) {
  1307. if (xic) {
  1308. // Not portable.
  1309. set_ime_position(Point2(0, 1));
  1310. }
  1311. if ((event->xconfigure.width == current_videomode.width) &&
  1312. (event->xconfigure.height == current_videomode.height))
  1313. return;
  1314. current_videomode.width = event->xconfigure.width;
  1315. current_videomode.height = event->xconfigure.height;
  1316. }
  1317. void OS_X11::process_xevents() {
  1318. //printf("checking events %i\n", XPending(x11_display));
  1319. do_mouse_warp = false;
  1320. // Is the current mouse mode one where it needs to be grabbed.
  1321. bool mouse_mode_grab = mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED;
  1322. while (XPending(x11_display) > 0) {
  1323. XEvent event;
  1324. XNextEvent(x11_display, &event);
  1325. if (XFilterEvent(&event, None)) {
  1326. continue;
  1327. }
  1328. #ifdef TOUCH_ENABLED
  1329. if (XGetEventData(x11_display, &event.xcookie)) {
  1330. if (event.xcookie.type == GenericEvent && event.xcookie.extension == touch.opcode) {
  1331. XIDeviceEvent *event_data = (XIDeviceEvent *)event.xcookie.data;
  1332. int index = event_data->detail;
  1333. Vector2 pos = Vector2(event_data->event_x, event_data->event_y);
  1334. switch (event_data->evtype) {
  1335. case XI_TouchBegin: // Fall-through
  1336. // Disabled hand-in-hand with the grabbing
  1337. //XIAllowTouchEvents(x11_display, event_data->deviceid, event_data->detail, x11_window, XIAcceptTouch);
  1338. case XI_TouchEnd: {
  1339. bool is_begin = event_data->evtype == XI_TouchBegin;
  1340. Ref<InputEventScreenTouch> st;
  1341. st.instance();
  1342. st->set_index(index);
  1343. st->set_position(pos);
  1344. st->set_pressed(is_begin);
  1345. if (is_begin) {
  1346. if (touch.state.has(index)) // Defensive
  1347. break;
  1348. touch.state[index] = pos;
  1349. if (touch.state.size() == 1) {
  1350. // X11 may send a motion event when a touch gesture begins, that would result
  1351. // in a spurious mouse motion event being sent to Godot; remember it to be able to filter it out
  1352. touch.mouse_pos_to_filter = pos;
  1353. }
  1354. input->parse_input_event(st);
  1355. } else {
  1356. if (!touch.state.has(index)) // Defensive
  1357. break;
  1358. touch.state.erase(index);
  1359. input->parse_input_event(st);
  1360. }
  1361. } break;
  1362. case XI_TouchUpdate: {
  1363. Map<int, Vector2>::Element *curr_pos_elem = touch.state.find(index);
  1364. if (!curr_pos_elem) { // Defensive
  1365. break;
  1366. }
  1367. if (curr_pos_elem->value() != pos) {
  1368. Ref<InputEventScreenDrag> sd;
  1369. sd.instance();
  1370. sd->set_index(index);
  1371. sd->set_position(pos);
  1372. sd->set_relative(pos - curr_pos_elem->value());
  1373. input->parse_input_event(sd);
  1374. curr_pos_elem->value() = pos;
  1375. }
  1376. } break;
  1377. }
  1378. }
  1379. }
  1380. XFreeEventData(x11_display, &event.xcookie);
  1381. #endif
  1382. switch (event.type) {
  1383. case Expose:
  1384. Main::force_redraw();
  1385. break;
  1386. case NoExpose:
  1387. minimized = true;
  1388. break;
  1389. case VisibilityNotify: {
  1390. XVisibilityEvent *visibility = (XVisibilityEvent *)&event;
  1391. minimized = (visibility->state == VisibilityFullyObscured);
  1392. } break;
  1393. case LeaveNotify: {
  1394. if (main_loop && !mouse_mode_grab)
  1395. main_loop->notification(MainLoop::NOTIFICATION_WM_MOUSE_EXIT);
  1396. if (input)
  1397. input->set_mouse_in_window(false);
  1398. } break;
  1399. case EnterNotify: {
  1400. if (main_loop && !mouse_mode_grab)
  1401. main_loop->notification(MainLoop::NOTIFICATION_WM_MOUSE_ENTER);
  1402. if (input)
  1403. input->set_mouse_in_window(true);
  1404. } break;
  1405. case FocusIn:
  1406. minimized = false;
  1407. window_has_focus = true;
  1408. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  1409. if (mouse_mode_grab) {
  1410. // Show and update the cursor if confined and the window regained focus.
  1411. if (mouse_mode == MOUSE_MODE_CONFINED)
  1412. XUndefineCursor(x11_display, x11_window);
  1413. else if (mouse_mode == MOUSE_MODE_CAPTURED) // or re-hide it in captured mode
  1414. XDefineCursor(x11_display, x11_window, null_cursor);
  1415. XGrabPointer(
  1416. x11_display, x11_window, True,
  1417. ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
  1418. GrabModeAsync, GrabModeAsync, x11_window, None, CurrentTime);
  1419. }
  1420. #ifdef TOUCH_ENABLED
  1421. // Grab touch devices to avoid OS gesture interference
  1422. /*for (int i = 0; i < touch.devices.size(); ++i) {
  1423. XIGrabDevice(x11_display, touch.devices[i], x11_window, CurrentTime, None, XIGrabModeAsync, XIGrabModeAsync, False, &touch.event_mask);
  1424. }*/
  1425. #endif
  1426. if (xic) {
  1427. XSetICFocus(xic);
  1428. }
  1429. break;
  1430. case FocusOut:
  1431. window_has_focus = false;
  1432. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  1433. if (mouse_mode_grab) {
  1434. //dear X11, I try, I really try, but you never work, you do whathever you want.
  1435. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  1436. // Show the cursor if we're in captured mode so it doesn't look weird.
  1437. XUndefineCursor(x11_display, x11_window);
  1438. }
  1439. XUngrabPointer(x11_display, CurrentTime);
  1440. }
  1441. #ifdef TOUCH_ENABLED
  1442. // Ungrab touch devices so input works as usual while we are unfocused
  1443. /*for (int i = 0; i < touch.devices.size(); ++i) {
  1444. XIUngrabDevice(x11_display, touch.devices[i], CurrentTime);
  1445. }*/
  1446. // Release every pointer to avoid sticky points
  1447. for (Map<int, Vector2>::Element *E = touch.state.front(); E; E = E->next()) {
  1448. Ref<InputEventScreenTouch> st;
  1449. st.instance();
  1450. st->set_index(E->key());
  1451. st->set_position(E->get());
  1452. input->parse_input_event(st);
  1453. }
  1454. touch.state.clear();
  1455. #endif
  1456. if (xic) {
  1457. XUnsetICFocus(xic);
  1458. }
  1459. break;
  1460. case ConfigureNotify:
  1461. _window_changed(&event);
  1462. break;
  1463. case ButtonPress:
  1464. case ButtonRelease: {
  1465. /* exit in case of a mouse button press */
  1466. last_timestamp = event.xbutton.time;
  1467. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  1468. event.xbutton.x = last_mouse_pos.x;
  1469. event.xbutton.y = last_mouse_pos.y;
  1470. }
  1471. Ref<InputEventMouseButton> mb;
  1472. mb.instance();
  1473. get_key_modifier_state(event.xbutton.state, mb);
  1474. mb->set_button_mask(get_mouse_button_state(event.xbutton.state));
  1475. mb->set_position(Vector2(event.xbutton.x, event.xbutton.y));
  1476. mb->set_global_position(mb->get_position());
  1477. mb->set_button_index(event.xbutton.button);
  1478. if (mb->get_button_index() == 2)
  1479. mb->set_button_index(3);
  1480. else if (mb->get_button_index() == 3)
  1481. mb->set_button_index(2);
  1482. mb->set_pressed((event.type == ButtonPress));
  1483. if (event.type == ButtonPress && event.xbutton.button == 1) {
  1484. uint64_t diff = get_ticks_usec() / 1000 - last_click_ms;
  1485. if (diff < 400 && Point2(last_click_pos).distance_to(Point2(event.xbutton.x, event.xbutton.y)) < 5) {
  1486. last_click_ms = 0;
  1487. last_click_pos = Point2(-100, -100);
  1488. mb->set_doubleclick(true);
  1489. } else {
  1490. last_click_ms += diff;
  1491. last_click_pos = Point2(event.xbutton.x, event.xbutton.y);
  1492. }
  1493. }
  1494. input->parse_input_event(mb);
  1495. } break;
  1496. case MotionNotify: {
  1497. // FUCK YOU X11 API YOU SERIOUSLY GROSS ME OUT
  1498. // YOU ARE AS GROSS AS LOOKING AT A PUTRID PILE
  1499. // OF POOP STICKING OUT OF A CLOGGED TOILET
  1500. // HOW THE FUCK I AM SUPPOSED TO KNOW WHICH ONE
  1501. // OF THE MOTION NOTIFY EVENTS IS THE ONE GENERATED
  1502. // BY WARPING THE MOUSE POINTER?
  1503. // YOU ARE FORCING ME TO FILTER ONE BY ONE TO FIND IT
  1504. // PLEASE DO ME A FAVOR AND DIE DROWNED IN A FECAL
  1505. // MOUNTAIN BECAUSE THAT'S WHERE YOU BELONG.
  1506. while (true) {
  1507. if (mouse_mode == MOUSE_MODE_CAPTURED && event.xmotion.x == current_videomode.width / 2 && event.xmotion.y == current_videomode.height / 2) {
  1508. //this is likely the warp event since it was warped here
  1509. center = Vector2(event.xmotion.x, event.xmotion.y);
  1510. break;
  1511. }
  1512. if (XPending(x11_display) > 0) {
  1513. XEvent tevent;
  1514. XPeekEvent(x11_display, &tevent);
  1515. if (tevent.type == MotionNotify) {
  1516. XNextEvent(x11_display, &event);
  1517. } else {
  1518. break;
  1519. }
  1520. } else {
  1521. break;
  1522. }
  1523. }
  1524. last_timestamp = event.xmotion.time;
  1525. // Motion is also simple.
  1526. // A little hack is in order
  1527. // to be able to send relative motion events.
  1528. Point2i pos(event.xmotion.x, event.xmotion.y);
  1529. // Avoidance of spurious mouse motion (see handling of touch)
  1530. bool filter = false;
  1531. // Adding some tolerance to match better Point2i to Vector2
  1532. if (touch.state.size() && Vector2(pos).distance_squared_to(touch.mouse_pos_to_filter) < 2) {
  1533. filter = true;
  1534. }
  1535. // Invalidate to avoid filtering a possible legitimate similar event coming later
  1536. touch.mouse_pos_to_filter = Vector2(1e10, 1e10);
  1537. if (filter) {
  1538. break;
  1539. }
  1540. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  1541. if (pos == Point2i(current_videomode.width / 2, current_videomode.height / 2)) {
  1542. //this sucks, it's a hack, etc and is a little inaccurate, etc.
  1543. //but nothing I can do, X11 sucks.
  1544. center = pos;
  1545. break;
  1546. }
  1547. Point2i new_center = pos;
  1548. pos = last_mouse_pos + (pos - center);
  1549. center = new_center;
  1550. do_mouse_warp = window_has_focus; // warp the cursor if we're focused in
  1551. }
  1552. if (!last_mouse_pos_valid) {
  1553. last_mouse_pos = pos;
  1554. last_mouse_pos_valid = true;
  1555. }
  1556. Point2i rel = pos - last_mouse_pos;
  1557. Ref<InputEventMouseMotion> mm;
  1558. mm.instance();
  1559. get_key_modifier_state(event.xmotion.state, mm);
  1560. mm->set_button_mask(get_mouse_button_state(event.xmotion.state));
  1561. mm->set_position(pos);
  1562. mm->set_global_position(pos);
  1563. input->set_mouse_position(pos);
  1564. mm->set_speed(input->get_last_mouse_speed());
  1565. mm->set_relative(rel);
  1566. last_mouse_pos = pos;
  1567. // printf("rel: %d,%d\n", rel.x, rel.y );
  1568. // Don't propagate the motion event unless we have focus
  1569. // this is so that the relative motion doesn't get messed up
  1570. // after we regain focus.
  1571. if (window_has_focus || !mouse_mode_grab)
  1572. input->parse_input_event(mm);
  1573. } break;
  1574. case KeyPress:
  1575. case KeyRelease: {
  1576. last_timestamp = event.xkey.time;
  1577. // key event is a little complex, so
  1578. // it will be handled in it's own function.
  1579. handle_key_event((XKeyEvent *)&event);
  1580. } break;
  1581. case SelectionRequest: {
  1582. XSelectionRequestEvent *req;
  1583. XEvent e, respond;
  1584. e = event;
  1585. req = &(e.xselectionrequest);
  1586. if (req->target == XInternAtom(x11_display, "UTF8_STRING", 0) ||
  1587. req->target == XInternAtom(x11_display, "COMPOUND_TEXT", 0) ||
  1588. req->target == XInternAtom(x11_display, "TEXT", 0) ||
  1589. req->target == XA_STRING ||
  1590. req->target == XInternAtom(x11_display, "text/plain;charset=utf-8", 0) ||
  1591. req->target == XInternAtom(x11_display, "text/plain", 0)) {
  1592. CharString clip = OS::get_clipboard().utf8();
  1593. XChangeProperty(x11_display,
  1594. req->requestor,
  1595. req->property,
  1596. req->target,
  1597. 8,
  1598. PropModeReplace,
  1599. (unsigned char *)clip.get_data(),
  1600. clip.length());
  1601. respond.xselection.property = req->property;
  1602. } else if (req->target == XInternAtom(x11_display, "TARGETS", 0)) {
  1603. Atom data[7];
  1604. data[0] = XInternAtom(x11_display, "TARGETS", 0);
  1605. data[1] = XInternAtom(x11_display, "UTF8_STRING", 0);
  1606. data[2] = XInternAtom(x11_display, "COMPOUND_TEXT", 0);
  1607. data[3] = XInternAtom(x11_display, "TEXT", 0);
  1608. data[4] = XA_STRING;
  1609. data[5] = XInternAtom(x11_display, "text/plain;charset=utf-8", 0);
  1610. data[6] = XInternAtom(x11_display, "text/plain", 0);
  1611. XChangeProperty(x11_display,
  1612. req->requestor,
  1613. req->property,
  1614. XA_ATOM,
  1615. 32,
  1616. PropModeReplace,
  1617. (unsigned char *)&data,
  1618. sizeof(data) / sizeof(data[0]));
  1619. respond.xselection.property = req->property;
  1620. } else {
  1621. char *targetname = XGetAtomName(x11_display, req->target);
  1622. printf("No Target '%s'\n", targetname);
  1623. if (targetname)
  1624. XFree(targetname);
  1625. respond.xselection.property = None;
  1626. }
  1627. respond.xselection.type = SelectionNotify;
  1628. respond.xselection.display = req->display;
  1629. respond.xselection.requestor = req->requestor;
  1630. respond.xselection.selection = req->selection;
  1631. respond.xselection.target = req->target;
  1632. respond.xselection.time = req->time;
  1633. XSendEvent(x11_display, req->requestor, True, NoEventMask, &respond);
  1634. XFlush(x11_display);
  1635. } break;
  1636. case SelectionNotify:
  1637. if (event.xselection.target == requested) {
  1638. Property p = read_property(x11_display, x11_window, XInternAtom(x11_display, "PRIMARY", 0));
  1639. Vector<String> files = String((char *)p.data).split("\n", false);
  1640. for (int i = 0; i < files.size(); i++) {
  1641. files[i] = files[i].replace("file://", "").replace("%20", " ").strip_escapes();
  1642. }
  1643. main_loop->drop_files(files);
  1644. //Reply that all is well.
  1645. XClientMessageEvent m;
  1646. memset(&m, 0, sizeof(m));
  1647. m.type = ClientMessage;
  1648. m.display = x11_display;
  1649. m.window = xdnd_source_window;
  1650. m.message_type = xdnd_finished;
  1651. m.format = 32;
  1652. m.data.l[0] = x11_window;
  1653. m.data.l[1] = 1;
  1654. m.data.l[2] = xdnd_action_copy; //We only ever copy.
  1655. XSendEvent(x11_display, xdnd_source_window, False, NoEventMask, (XEvent *)&m);
  1656. }
  1657. break;
  1658. case ClientMessage:
  1659. if ((unsigned int)event.xclient.data.l[0] == (unsigned int)wm_delete)
  1660. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  1661. else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_enter) {
  1662. //File(s) have been dragged over the window, check for supported target (text/uri-list)
  1663. xdnd_version = (event.xclient.data.l[1] >> 24);
  1664. Window source = event.xclient.data.l[0];
  1665. bool more_than_3 = event.xclient.data.l[1] & 1;
  1666. if (more_than_3) {
  1667. Property p = read_property(x11_display, source, XInternAtom(x11_display, "XdndTypeList", False));
  1668. requested = pick_target_from_list(x11_display, (Atom *)p.data, p.nitems);
  1669. } else
  1670. requested = pick_target_from_atoms(x11_display, event.xclient.data.l[2], event.xclient.data.l[3], event.xclient.data.l[4]);
  1671. } else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_position) {
  1672. //xdnd position event, reply with an XDND status message
  1673. //just depending on type of data for now
  1674. XClientMessageEvent m;
  1675. memset(&m, 0, sizeof(m));
  1676. m.type = ClientMessage;
  1677. m.display = event.xclient.display;
  1678. m.window = event.xclient.data.l[0];
  1679. m.message_type = xdnd_status;
  1680. m.format = 32;
  1681. m.data.l[0] = x11_window;
  1682. m.data.l[1] = (requested != None);
  1683. m.data.l[2] = 0; //empty rectangle
  1684. m.data.l[3] = 0;
  1685. m.data.l[4] = xdnd_action_copy;
  1686. XSendEvent(x11_display, event.xclient.data.l[0], False, NoEventMask, (XEvent *)&m);
  1687. XFlush(x11_display);
  1688. } else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_drop) {
  1689. if (requested != None) {
  1690. xdnd_source_window = event.xclient.data.l[0];
  1691. if (xdnd_version >= 1)
  1692. XConvertSelection(x11_display, xdnd_selection, requested, XInternAtom(x11_display, "PRIMARY", 0), x11_window, event.xclient.data.l[2]);
  1693. else
  1694. XConvertSelection(x11_display, xdnd_selection, requested, XInternAtom(x11_display, "PRIMARY", 0), x11_window, CurrentTime);
  1695. } else {
  1696. //Reply that we're not interested.
  1697. XClientMessageEvent m;
  1698. memset(&m, 0, sizeof(m));
  1699. m.type = ClientMessage;
  1700. m.display = event.xclient.display;
  1701. m.window = event.xclient.data.l[0];
  1702. m.message_type = xdnd_finished;
  1703. m.format = 32;
  1704. m.data.l[0] = x11_window;
  1705. m.data.l[1] = 0;
  1706. m.data.l[2] = None; //Failed.
  1707. XSendEvent(x11_display, event.xclient.data.l[0], False, NoEventMask, (XEvent *)&m);
  1708. }
  1709. }
  1710. break;
  1711. default:
  1712. break;
  1713. }
  1714. }
  1715. XFlush(x11_display);
  1716. if (do_mouse_warp) {
  1717. XWarpPointer(x11_display, None, x11_window,
  1718. 0, 0, 0, 0, (int)current_videomode.width / 2, (int)current_videomode.height / 2);
  1719. /*
  1720. Window root, child;
  1721. int root_x, root_y;
  1722. int win_x, win_y;
  1723. unsigned int mask;
  1724. XQueryPointer( x11_display, x11_window, &root, &child, &root_x, &root_y, &win_x, &win_y, &mask );
  1725. printf("Root: %d,%d\n", root_x, root_y);
  1726. printf("Win: %d,%d\n", win_x, win_y);
  1727. */
  1728. }
  1729. }
  1730. MainLoop *OS_X11::get_main_loop() const {
  1731. return main_loop;
  1732. }
  1733. void OS_X11::delete_main_loop() {
  1734. if (main_loop)
  1735. memdelete(main_loop);
  1736. main_loop = NULL;
  1737. }
  1738. void OS_X11::set_main_loop(MainLoop *p_main_loop) {
  1739. main_loop = p_main_loop;
  1740. input->set_main_loop(p_main_loop);
  1741. }
  1742. bool OS_X11::can_draw() const {
  1743. return !minimized;
  1744. };
  1745. void OS_X11::set_clipboard(const String &p_text) {
  1746. OS::set_clipboard(p_text);
  1747. XSetSelectionOwner(x11_display, XA_PRIMARY, x11_window, CurrentTime);
  1748. XSetSelectionOwner(x11_display, XInternAtom(x11_display, "CLIPBOARD", 0), x11_window, CurrentTime);
  1749. };
  1750. static String _get_clipboard_impl(Atom p_source, Window x11_window, ::Display *x11_display, String p_internal_clipboard, Atom target) {
  1751. String ret;
  1752. Atom type;
  1753. Atom selection = XA_PRIMARY;
  1754. int format, result;
  1755. unsigned long len, bytes_left, dummy;
  1756. unsigned char *data;
  1757. Window Sown = XGetSelectionOwner(x11_display, p_source);
  1758. if (Sown == x11_window) {
  1759. return p_internal_clipboard;
  1760. };
  1761. if (Sown != None) {
  1762. XConvertSelection(x11_display, p_source, target, selection,
  1763. x11_window, CurrentTime);
  1764. XFlush(x11_display);
  1765. while (true) {
  1766. XEvent event;
  1767. XNextEvent(x11_display, &event);
  1768. if (event.type == SelectionNotify && event.xselection.requestor == x11_window) {
  1769. break;
  1770. };
  1771. };
  1772. //
  1773. // Do not get any data, see how much data is there
  1774. //
  1775. XGetWindowProperty(x11_display, x11_window,
  1776. selection, // Tricky..
  1777. 0, 0, // offset - len
  1778. 0, // Delete 0==FALSE
  1779. AnyPropertyType, //flag
  1780. &type, // return type
  1781. &format, // return format
  1782. &len, &bytes_left, //that
  1783. &data);
  1784. // DATA is There
  1785. if (bytes_left > 0) {
  1786. result = XGetWindowProperty(x11_display, x11_window,
  1787. selection, 0, bytes_left, 0,
  1788. AnyPropertyType, &type, &format,
  1789. &len, &dummy, &data);
  1790. if (result == Success) {
  1791. ret.parse_utf8((const char *)data);
  1792. } else
  1793. printf("FAIL\n");
  1794. XFree(data);
  1795. }
  1796. }
  1797. return ret;
  1798. }
  1799. static String _get_clipboard(Atom p_source, Window x11_window, ::Display *x11_display, String p_internal_clipboard) {
  1800. String ret;
  1801. Atom utf8_atom = XInternAtom(x11_display, "UTF8_STRING", True);
  1802. if (utf8_atom != None) {
  1803. ret = _get_clipboard_impl(p_source, x11_window, x11_display, p_internal_clipboard, utf8_atom);
  1804. }
  1805. if (ret == "") {
  1806. ret = _get_clipboard_impl(p_source, x11_window, x11_display, p_internal_clipboard, XA_STRING);
  1807. }
  1808. return ret;
  1809. }
  1810. String OS_X11::get_clipboard() const {
  1811. String ret;
  1812. ret = _get_clipboard(XInternAtom(x11_display, "CLIPBOARD", 0), x11_window, x11_display, OS::get_clipboard());
  1813. if (ret == "") {
  1814. ret = _get_clipboard(XA_PRIMARY, x11_window, x11_display, OS::get_clipboard());
  1815. };
  1816. return ret;
  1817. }
  1818. String OS_X11::get_name() {
  1819. return "X11";
  1820. }
  1821. Error OS_X11::shell_open(String p_uri) {
  1822. Error ok;
  1823. List<String> args;
  1824. args.push_back(p_uri);
  1825. ok = execute("xdg-open", args, false);
  1826. if (ok == OK)
  1827. return OK;
  1828. ok = execute("gnome-open", args, false);
  1829. if (ok == OK)
  1830. return OK;
  1831. ok = execute("kde-open", args, false);
  1832. return ok;
  1833. }
  1834. bool OS_X11::_check_internal_feature_support(const String &p_feature) {
  1835. return p_feature == "pc" || p_feature == "s3tc";
  1836. }
  1837. String OS_X11::get_config_path() const {
  1838. if (has_environment("XDG_CONFIG_HOME")) {
  1839. return get_environment("XDG_CONFIG_HOME");
  1840. } else if (has_environment("HOME")) {
  1841. return get_environment("HOME").plus_file(".config");
  1842. } else {
  1843. return ".";
  1844. }
  1845. }
  1846. String OS_X11::get_data_path() const {
  1847. if (has_environment("XDG_DATA_HOME")) {
  1848. return get_environment("XDG_DATA_HOME");
  1849. } else if (has_environment("HOME")) {
  1850. return get_environment("HOME").plus_file(".local/share");
  1851. } else {
  1852. return get_config_path();
  1853. }
  1854. }
  1855. String OS_X11::get_cache_path() const {
  1856. if (has_environment("XDG_CACHE_HOME")) {
  1857. return get_environment("XDG_CACHE_HOME");
  1858. } else if (has_environment("HOME")) {
  1859. return get_environment("HOME").plus_file(".cache");
  1860. } else {
  1861. return get_config_path();
  1862. }
  1863. }
  1864. String OS_X11::get_system_dir(SystemDir p_dir) const {
  1865. String xdgparam;
  1866. switch (p_dir) {
  1867. case SYSTEM_DIR_DESKTOP: {
  1868. xdgparam = "DESKTOP";
  1869. } break;
  1870. case SYSTEM_DIR_DCIM: {
  1871. xdgparam = "PICTURES";
  1872. } break;
  1873. case SYSTEM_DIR_DOCUMENTS: {
  1874. xdgparam = "DOCUMENTS";
  1875. } break;
  1876. case SYSTEM_DIR_DOWNLOADS: {
  1877. xdgparam = "DOWNLOAD";
  1878. } break;
  1879. case SYSTEM_DIR_MOVIES: {
  1880. xdgparam = "VIDEOS";
  1881. } break;
  1882. case SYSTEM_DIR_MUSIC: {
  1883. xdgparam = "MUSIC";
  1884. } break;
  1885. case SYSTEM_DIR_PICTURES: {
  1886. xdgparam = "PICTURES";
  1887. } break;
  1888. case SYSTEM_DIR_RINGTONES: {
  1889. xdgparam = "MUSIC";
  1890. } break;
  1891. }
  1892. String pipe;
  1893. List<String> arg;
  1894. arg.push_back(xdgparam);
  1895. Error err = const_cast<OS_X11 *>(this)->execute("xdg-user-dir", arg, true, NULL, &pipe);
  1896. if (err != OK)
  1897. return ".";
  1898. return pipe.strip_edges();
  1899. }
  1900. void OS_X11::move_window_to_foreground() {
  1901. XRaiseWindow(x11_display, x11_window);
  1902. }
  1903. void OS_X11::set_cursor_shape(CursorShape p_shape) {
  1904. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  1905. if (p_shape == current_cursor)
  1906. return;
  1907. if (mouse_mode == MOUSE_MODE_VISIBLE) {
  1908. if (cursors[p_shape] != None)
  1909. XDefineCursor(x11_display, x11_window, cursors[p_shape]);
  1910. else if (cursors[CURSOR_ARROW] != None)
  1911. XDefineCursor(x11_display, x11_window, cursors[CURSOR_ARROW]);
  1912. }
  1913. current_cursor = p_shape;
  1914. }
  1915. void OS_X11::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  1916. if (p_cursor.is_valid()) {
  1917. Ref<Texture> texture = p_cursor;
  1918. Ref<Image> image = texture->get_data();
  1919. ERR_FAIL_COND(texture->get_width() != 32 || texture->get_height() != 32);
  1920. // Create the cursor structure
  1921. XcursorImage *cursor_image = XcursorImageCreate(texture->get_width(), texture->get_height());
  1922. XcursorUInt image_size = 32 * 32;
  1923. XcursorDim size = sizeof(XcursorPixel) * image_size;
  1924. cursor_image->version = 1;
  1925. cursor_image->size = size;
  1926. cursor_image->xhot = p_hotspot.x;
  1927. cursor_image->yhot = p_hotspot.y;
  1928. // allocate memory to contain the whole file
  1929. cursor_image->pixels = (XcursorPixel *)malloc(size);
  1930. image->lock();
  1931. for (XcursorPixel index = 0; index < image_size; index++) {
  1932. int column_index = floor(index / 32);
  1933. int row_index = index % 32;
  1934. *(cursor_image->pixels + index) = image->get_pixel(row_index, column_index).to_argb32();
  1935. }
  1936. image->unlock();
  1937. ERR_FAIL_COND(cursor_image->pixels == NULL);
  1938. // Save it for a further usage
  1939. cursors[p_shape] = XcursorImageLoadCursor(x11_display, cursor_image);
  1940. if (p_shape == CURSOR_ARROW) {
  1941. XDefineCursor(x11_display, x11_window, cursors[p_shape]);
  1942. }
  1943. }
  1944. }
  1945. void OS_X11::release_rendering_thread() {
  1946. context_gl->release_current();
  1947. }
  1948. void OS_X11::make_rendering_thread() {
  1949. context_gl->make_current();
  1950. }
  1951. void OS_X11::swap_buffers() {
  1952. context_gl->swap_buffers();
  1953. }
  1954. void OS_X11::alert(const String &p_alert, const String &p_title) {
  1955. List<String> args;
  1956. args.push_back("-center");
  1957. args.push_back("-title");
  1958. args.push_back(p_title);
  1959. args.push_back(p_alert);
  1960. execute("xmessage", args, true);
  1961. }
  1962. void OS_X11::set_icon(const Ref<Image> &p_icon) {
  1963. Atom net_wm_icon = XInternAtom(x11_display, "_NET_WM_ICON", False);
  1964. if (p_icon.is_valid()) {
  1965. Ref<Image> img = p_icon->duplicate();
  1966. img->convert(Image::FORMAT_RGBA8);
  1967. int w = img->get_width();
  1968. int h = img->get_height();
  1969. // We're using long to have wordsize (32Bit build -> 32 Bits, 64 Bit build -> 64 Bits
  1970. Vector<long> pd;
  1971. pd.resize(2 + w * h);
  1972. pd[0] = w;
  1973. pd[1] = h;
  1974. PoolVector<uint8_t>::Read r = img->get_data().read();
  1975. long *wr = &pd[2];
  1976. uint8_t const *pr = r.ptr();
  1977. for (int i = 0; i < w * h; i++) {
  1978. long v = 0;
  1979. // A R G B
  1980. v |= pr[3] << 24 | pr[0] << 16 | pr[1] << 8 | pr[2];
  1981. *wr++ = v;
  1982. pr += 4;
  1983. }
  1984. XChangeProperty(x11_display, x11_window, net_wm_icon, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)pd.ptr(), pd.size());
  1985. } else {
  1986. XDeleteProperty(x11_display, x11_window, net_wm_icon);
  1987. }
  1988. XFlush(x11_display);
  1989. }
  1990. void OS_X11::force_process_input() {
  1991. process_xevents(); // get rid of pending events
  1992. #ifdef JOYDEV_ENABLED
  1993. joypad->process_joypads();
  1994. #endif
  1995. }
  1996. void OS_X11::run() {
  1997. force_quit = false;
  1998. if (!main_loop)
  1999. return;
  2000. main_loop->init();
  2001. //uint64_t last_ticks=get_ticks_usec();
  2002. //int frames=0;
  2003. //uint64_t frame=0;
  2004. while (!force_quit) {
  2005. process_xevents(); // get rid of pending events
  2006. #ifdef JOYDEV_ENABLED
  2007. joypad->process_joypads();
  2008. #endif
  2009. if (Main::iteration() == true)
  2010. break;
  2011. };
  2012. main_loop->finish();
  2013. }
  2014. bool OS_X11::is_joy_known(int p_device) {
  2015. return input->is_joy_mapped(p_device);
  2016. }
  2017. String OS_X11::get_joy_guid(int p_device) const {
  2018. return input->get_joy_guid_remapped(p_device);
  2019. }
  2020. void OS_X11::_set_use_vsync(bool p_enable) {
  2021. if (context_gl)
  2022. return context_gl->set_use_vsync(p_enable);
  2023. }
  2024. /*
  2025. bool OS_X11::is_vsync_enabled() const {
  2026. if (context_gl)
  2027. return context_gl->is_using_vsync();
  2028. return true;
  2029. }
  2030. */
  2031. void OS_X11::set_context(int p_context) {
  2032. XClassHint *classHint = XAllocClassHint();
  2033. if (classHint) {
  2034. if (p_context == CONTEXT_EDITOR)
  2035. classHint->res_name = (char *)"Godot_Editor";
  2036. if (p_context == CONTEXT_PROJECTMAN)
  2037. classHint->res_name = (char *)"Godot_ProjectList";
  2038. classHint->res_class = (char *)"Godot";
  2039. XSetClassHint(x11_display, x11_window, classHint);
  2040. XFree(classHint);
  2041. }
  2042. }
  2043. OS::PowerState OS_X11::get_power_state() {
  2044. return power_manager->get_power_state();
  2045. }
  2046. int OS_X11::get_power_seconds_left() {
  2047. return power_manager->get_power_seconds_left();
  2048. }
  2049. int OS_X11::get_power_percent_left() {
  2050. return power_manager->get_power_percent_left();
  2051. }
  2052. void OS_X11::disable_crash_handler() {
  2053. crash_handler.disable();
  2054. }
  2055. bool OS_X11::is_disable_crash_handler() const {
  2056. return crash_handler.is_disabled();
  2057. }
  2058. static String get_mountpoint(const String &p_path) {
  2059. struct stat s;
  2060. if (stat(p_path.utf8().get_data(), &s)) {
  2061. return "";
  2062. }
  2063. #ifdef HAVE_MNTENT
  2064. dev_t dev = s.st_dev;
  2065. FILE *fd = setmntent("/proc/mounts", "r");
  2066. if (!fd) {
  2067. return "";
  2068. }
  2069. struct mntent mnt;
  2070. char buf[1024];
  2071. size_t buflen = 1024;
  2072. while (getmntent_r(fd, &mnt, buf, buflen)) {
  2073. if (!stat(mnt.mnt_dir, &s) && s.st_dev == dev) {
  2074. endmntent(fd);
  2075. return String(mnt.mnt_dir);
  2076. }
  2077. }
  2078. endmntent(fd);
  2079. #endif
  2080. return "";
  2081. }
  2082. Error OS_X11::move_to_trash(const String &p_path) {
  2083. String trash_can = "";
  2084. String mnt = get_mountpoint(p_path);
  2085. // If there is a directory "[Mountpoint]/.Trash-[UID]/files", use it as the trash can.
  2086. if (mnt != "") {
  2087. String path(mnt + "/.Trash-" + itos(getuid()) + "/files");
  2088. struct stat s;
  2089. if (!stat(path.utf8().get_data(), &s)) {
  2090. trash_can = path;
  2091. }
  2092. }
  2093. // Otherwise, if ${XDG_DATA_HOME} is defined, use "${XDG_DATA_HOME}/Trash/files" as the trash can.
  2094. if (trash_can == "") {
  2095. char *dhome = getenv("XDG_DATA_HOME");
  2096. if (dhome) {
  2097. trash_can = String(dhome) + "/Trash/files";
  2098. }
  2099. }
  2100. // Otherwise, if ${HOME} is defined, use "${HOME}/.local/share/Trash/files" as the trash can.
  2101. if (trash_can == "") {
  2102. char *home = getenv("HOME");
  2103. if (home) {
  2104. trash_can = String(home) + "/.local/share/Trash/files";
  2105. }
  2106. }
  2107. // Issue an error if none of the previous locations is appropriate for the trash can.
  2108. if (trash_can == "") {
  2109. ERR_PRINTS("move_to_trash: Could not determine the trash can location");
  2110. return FAILED;
  2111. }
  2112. // Create needed directories for decided trash can location.
  2113. DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2114. Error err = dir_access->make_dir_recursive(trash_can);
  2115. memdelete(dir_access);
  2116. // Issue an error if trash can is not created proprely.
  2117. if (err != OK) {
  2118. ERR_PRINTS("move_to_trash: Could not create the trash can \"" + trash_can + "\"");
  2119. return err;
  2120. }
  2121. // The trash can is successfully created, now move the given resource to it.
  2122. // Do not use DirAccess:rename() because it can't move files across multiple mountpoints.
  2123. List<String> mv_args;
  2124. mv_args.push_back(p_path);
  2125. mv_args.push_back(trash_can);
  2126. int retval;
  2127. err = execute("mv", mv_args, true, NULL, NULL, &retval);
  2128. // Issue an error if "mv" failed to move the given resource to the trash can.
  2129. if (err != OK || retval != 0) {
  2130. ERR_PRINTS("move_to_trash: Could not move the resource \"" + p_path + "\" to the trash can \"" + trash_can + "\"");
  2131. return FAILED;
  2132. }
  2133. return OK;
  2134. }
  2135. OS::LatinKeyboardVariant OS_X11::get_latin_keyboard_variant() const {
  2136. XkbDescRec *xkbdesc = XkbAllocKeyboard();
  2137. ERR_FAIL_COND_V(!xkbdesc, LATIN_KEYBOARD_QWERTY);
  2138. XkbGetNames(x11_display, XkbSymbolsNameMask, xkbdesc);
  2139. ERR_FAIL_COND_V(!xkbdesc->names, LATIN_KEYBOARD_QWERTY);
  2140. ERR_FAIL_COND_V(!xkbdesc->names->symbols, LATIN_KEYBOARD_QWERTY);
  2141. char *layout = XGetAtomName(x11_display, xkbdesc->names->symbols);
  2142. ERR_FAIL_COND_V(!layout, LATIN_KEYBOARD_QWERTY);
  2143. Vector<String> info = String(layout).split("+");
  2144. ERR_FAIL_INDEX_V(1, info.size(), LATIN_KEYBOARD_QWERTY);
  2145. if (info[1].find("colemak") != -1) {
  2146. return LATIN_KEYBOARD_COLEMAK;
  2147. } else if (info[1].find("qwertz") != -1) {
  2148. return LATIN_KEYBOARD_QWERTZ;
  2149. } else if (info[1].find("azerty") != -1) {
  2150. return LATIN_KEYBOARD_AZERTY;
  2151. } else if (info[1].find("qzerty") != -1) {
  2152. return LATIN_KEYBOARD_QZERTY;
  2153. } else if (info[1].find("dvorak") != -1) {
  2154. return LATIN_KEYBOARD_DVORAK;
  2155. } else if (info[1].find("neo") != -1) {
  2156. return LATIN_KEYBOARD_NEO;
  2157. }
  2158. return LATIN_KEYBOARD_QWERTY;
  2159. }
  2160. OS_X11::OS_X11() {
  2161. #ifdef PULSEAUDIO_ENABLED
  2162. AudioDriverManager::add_driver(&driver_pulseaudio);
  2163. #endif
  2164. #ifdef ALSA_ENABLED
  2165. AudioDriverManager::add_driver(&driver_alsa);
  2166. #endif
  2167. minimized = false;
  2168. xim_style = 0L;
  2169. mouse_mode = MOUSE_MODE_VISIBLE;
  2170. }