SDL_x11window.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_VIDEO_DRIVER_X11
  20. #include "../SDL_sysvideo.h"
  21. #include "../SDL_pixels_c.h"
  22. #include "../../events/SDL_keyboard_c.h"
  23. #include "../../events/SDL_mouse_c.h"
  24. #include "../../events/SDL_events_c.h"
  25. #include "../../core/unix/SDL_appid.h"
  26. #include "SDL_x11video.h"
  27. #include "SDL_x11mouse.h"
  28. #include "SDL_x11xinput2.h"
  29. #include "SDL_x11xfixes.h"
  30. #ifdef SDL_VIDEO_OPENGL_EGL
  31. #include "SDL_x11opengles.h"
  32. #endif
  33. #include "SDL_x11xsync.h"
  34. #define _NET_WM_STATE_REMOVE 0l
  35. #define _NET_WM_STATE_ADD 1l
  36. #define CHECK_WINDOW_DATA(window) \
  37. if (!window) { \
  38. return SDL_SetError("Invalid window"); \
  39. } \
  40. if (!window->internal) { \
  41. return SDL_SetError("Invalid window driver data"); \
  42. }
  43. #define CHECK_DISPLAY_DATA(display) \
  44. if (!_display) { \
  45. return SDL_SetError("Invalid display"); \
  46. } \
  47. if (!_display->internal) { \
  48. return SDL_SetError("Invalid display driver data"); \
  49. }
  50. static Bool isMapNotify(Display *dpy, XEvent *ev, XPointer win) // NOLINT(readability-non-const-parameter): cannot make XPointer a const pointer due to typedef
  51. {
  52. return ev->type == MapNotify && ev->xmap.window == *((Window *)win);
  53. }
  54. static Bool isUnmapNotify(Display *dpy, XEvent *ev, XPointer win) // NOLINT(readability-non-const-parameter): cannot make XPointer a const pointer due to typedef
  55. {
  56. return ev->type == UnmapNotify && ev->xunmap.window == *((Window *)win);
  57. }
  58. /*
  59. static Bool isConfigureNotify(Display *dpy, XEvent *ev, XPointer win)
  60. {
  61. return ev->type == ConfigureNotify && ev->xconfigure.window == *((Window*)win);
  62. }
  63. static Bool X11_XIfEventTimeout(Display *display, XEvent *event_return, Bool (*predicate)(), XPointer arg, int timeoutMS)
  64. {
  65. Uint64 start = SDL_GetTicks();
  66. while (!X11_XCheckIfEvent(display, event_return, predicate, arg)) {
  67. if (SDL_GetTicks() >= (start + timeoutMS)) {
  68. return False;
  69. }
  70. }
  71. return True;
  72. }
  73. */
  74. static bool X11_IsWindowMapped(SDL_VideoDevice *_this, SDL_Window *window)
  75. {
  76. SDL_WindowData *data = window->internal;
  77. SDL_VideoData *videodata = _this->internal;
  78. XWindowAttributes attr;
  79. X11_XGetWindowAttributes(videodata->display, data->xwindow, &attr);
  80. if (attr.map_state != IsUnmapped) {
  81. return true;
  82. } else {
  83. return false;
  84. }
  85. }
  86. #if 0
  87. static bool X11_IsActionAllowed(SDL_Window *window, Atom action)
  88. {
  89. SDL_WindowData *data = window->internal;
  90. Atom _NET_WM_ALLOWED_ACTIONS = data->videodata->_NET_WM_ALLOWED_ACTIONS;
  91. Atom type;
  92. Display *display = data->videodata->display;
  93. int form;
  94. unsigned long remain;
  95. unsigned long len, i;
  96. Atom *list;
  97. bool ret = false;
  98. if (X11_XGetWindowProperty(display, data->xwindow, _NET_WM_ALLOWED_ACTIONS, 0, 1024, False, XA_ATOM, &type, &form, &len, &remain, (unsigned char **)&list) == Success) {
  99. for (i=0; i<len; ++i) {
  100. if (list[i] == action) {
  101. ret = true;
  102. break;
  103. }
  104. }
  105. X11_XFree(list);
  106. }
  107. return ret;
  108. }
  109. #endif // 0
  110. static void X11_FlushPendingEvents(SDL_VideoDevice *_this, SDL_Window *window)
  111. {
  112. // Serialize and restore the pending flags, as they may be overwritten while flushing.
  113. const bool last_position_pending = window->last_position_pending;
  114. const bool last_size_pending = window->last_size_pending;
  115. X11_SyncWindow(_this, window);
  116. window->last_position_pending = last_position_pending;
  117. window->last_size_pending = last_size_pending;
  118. }
  119. void X11_SetNetWMState(SDL_VideoDevice *_this, Window xwindow, SDL_WindowFlags flags)
  120. {
  121. SDL_VideoData *videodata = _this->internal;
  122. Display *display = videodata->display;
  123. // !!! FIXME: just dereference videodata below instead of copying to locals.
  124. Atom _NET_WM_STATE = videodata->atoms._NET_WM_STATE;
  125. // Atom _NET_WM_STATE_HIDDEN = videodata->atoms._NET_WM_STATE_HIDDEN;
  126. Atom _NET_WM_STATE_FOCUSED = videodata->atoms._NET_WM_STATE_FOCUSED;
  127. Atom _NET_WM_STATE_MAXIMIZED_VERT = videodata->atoms._NET_WM_STATE_MAXIMIZED_VERT;
  128. Atom _NET_WM_STATE_MAXIMIZED_HORZ = videodata->atoms._NET_WM_STATE_MAXIMIZED_HORZ;
  129. Atom _NET_WM_STATE_FULLSCREEN = videodata->atoms._NET_WM_STATE_FULLSCREEN;
  130. Atom _NET_WM_STATE_ABOVE = videodata->atoms._NET_WM_STATE_ABOVE;
  131. Atom _NET_WM_STATE_SKIP_TASKBAR = videodata->atoms._NET_WM_STATE_SKIP_TASKBAR;
  132. Atom _NET_WM_STATE_SKIP_PAGER = videodata->atoms._NET_WM_STATE_SKIP_PAGER;
  133. Atom _NET_WM_STATE_MODAL = videodata->atoms._NET_WM_STATE_MODAL;
  134. Atom atoms[16];
  135. int count = 0;
  136. /* The window manager sets this property, we shouldn't set it.
  137. If we did, this would indicate to the window manager that we don't
  138. actually want to be mapped during X11_XMapRaised(), which would be bad.
  139. *
  140. if ((flags & SDL_WINDOW_HIDDEN) != 0) {
  141. atoms[count++] = _NET_WM_STATE_HIDDEN;
  142. }
  143. */
  144. if (flags & SDL_WINDOW_ALWAYS_ON_TOP) {
  145. atoms[count++] = _NET_WM_STATE_ABOVE;
  146. }
  147. if (flags & SDL_WINDOW_UTILITY) {
  148. atoms[count++] = _NET_WM_STATE_SKIP_TASKBAR;
  149. atoms[count++] = _NET_WM_STATE_SKIP_PAGER;
  150. }
  151. if (flags & SDL_WINDOW_INPUT_FOCUS) {
  152. atoms[count++] = _NET_WM_STATE_FOCUSED;
  153. }
  154. if (flags & SDL_WINDOW_MAXIMIZED) {
  155. atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
  156. atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
  157. }
  158. if (flags & SDL_WINDOW_FULLSCREEN) {
  159. atoms[count++] = _NET_WM_STATE_FULLSCREEN;
  160. }
  161. if (flags & SDL_WINDOW_MODAL) {
  162. atoms[count++] = _NET_WM_STATE_MODAL;
  163. }
  164. SDL_assert(count <= SDL_arraysize(atoms));
  165. if (count > 0) {
  166. X11_XChangeProperty(display, xwindow, _NET_WM_STATE, XA_ATOM, 32,
  167. PropModeReplace, (unsigned char *)atoms, count);
  168. } else {
  169. X11_XDeleteProperty(display, xwindow, _NET_WM_STATE);
  170. }
  171. }
  172. static void X11_ConstrainPopup(SDL_Window *window, bool output_to_pending)
  173. {
  174. // Clamp popup windows to the output borders
  175. if (SDL_WINDOW_IS_POPUP(window)) {
  176. SDL_Window *w;
  177. SDL_DisplayID displayID;
  178. SDL_Rect rect;
  179. int abs_x = window->last_position_pending ? window->pending.x : window->floating.x;
  180. int abs_y = window->last_position_pending ? window->pending.y : window->floating.y;
  181. int offset_x = 0, offset_y = 0;
  182. // Calculate the total offset from the parents
  183. for (w = window->parent; SDL_WINDOW_IS_POPUP(w); w = w->parent) {
  184. offset_x += w->x;
  185. offset_y += w->y;
  186. }
  187. offset_x += w->x;
  188. offset_y += w->y;
  189. abs_x += offset_x;
  190. abs_y += offset_y;
  191. displayID = SDL_GetDisplayForWindow(w);
  192. SDL_GetDisplayBounds(displayID, &rect);
  193. if (abs_x + window->w > rect.x + rect.w) {
  194. abs_x -= (abs_x + window->w) - (rect.x + rect.w);
  195. }
  196. if (abs_y + window->h > rect.y + rect.h) {
  197. abs_y -= (abs_y + window->h) - (rect.y + rect.h);
  198. }
  199. abs_x = SDL_max(abs_x, rect.x);
  200. abs_y = SDL_max(abs_y, rect.y);
  201. if (output_to_pending) {
  202. window->pending.x = abs_x - offset_x;
  203. window->pending.y = abs_y - offset_y;
  204. } else {
  205. window->floating.x = window->windowed.x = abs_x - offset_x;
  206. window->floating.y = window->windowed.y = abs_y - offset_y;
  207. }
  208. }
  209. }
  210. static void X11_SetKeyboardFocus(SDL_Window *window, bool set_active_focus)
  211. {
  212. SDL_Window *toplevel = window;
  213. // Find the toplevel parent
  214. while (SDL_WINDOW_IS_POPUP(toplevel)) {
  215. toplevel = toplevel->parent;
  216. }
  217. toplevel->internal->keyboard_focus = window;
  218. if (set_active_focus && !window->is_hiding && !window->is_destroying) {
  219. SDL_SetKeyboardFocus(window);
  220. }
  221. }
  222. Uint32 X11_GetNetWMState(SDL_VideoDevice *_this, SDL_Window *window, Window xwindow)
  223. {
  224. SDL_VideoData *videodata = _this->internal;
  225. Display *display = videodata->display;
  226. Atom _NET_WM_STATE = videodata->atoms._NET_WM_STATE;
  227. Atom _NET_WM_STATE_HIDDEN = videodata->atoms._NET_WM_STATE_HIDDEN;
  228. Atom _NET_WM_STATE_FOCUSED = videodata->atoms._NET_WM_STATE_FOCUSED;
  229. Atom _NET_WM_STATE_MAXIMIZED_VERT = videodata->atoms._NET_WM_STATE_MAXIMIZED_VERT;
  230. Atom _NET_WM_STATE_MAXIMIZED_HORZ = videodata->atoms._NET_WM_STATE_MAXIMIZED_HORZ;
  231. Atom _NET_WM_STATE_FULLSCREEN = videodata->atoms._NET_WM_STATE_FULLSCREEN;
  232. Atom actualType;
  233. int actualFormat;
  234. unsigned long i, numItems, bytesAfter;
  235. unsigned char *propertyValue = NULL;
  236. long maxLength = 1024;
  237. SDL_WindowFlags flags = 0;
  238. if (X11_XGetWindowProperty(display, xwindow, _NET_WM_STATE,
  239. 0l, maxLength, False, XA_ATOM, &actualType,
  240. &actualFormat, &numItems, &bytesAfter,
  241. &propertyValue) == Success) {
  242. Atom *atoms = (Atom *)propertyValue;
  243. int maximized = 0;
  244. int fullscreen = 0;
  245. for (i = 0; i < numItems; ++i) {
  246. if (atoms[i] == _NET_WM_STATE_HIDDEN) {
  247. flags |= SDL_WINDOW_MINIMIZED | SDL_WINDOW_OCCLUDED;
  248. } else if (atoms[i] == _NET_WM_STATE_FOCUSED) {
  249. flags |= SDL_WINDOW_INPUT_FOCUS;
  250. } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_VERT) {
  251. maximized |= 1;
  252. } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_HORZ) {
  253. maximized |= 2;
  254. } else if (atoms[i] == _NET_WM_STATE_FULLSCREEN) {
  255. fullscreen = 1;
  256. }
  257. }
  258. if (fullscreen == 1) {
  259. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  260. // Pick whatever state the window expects
  261. flags |= (window->flags & SDL_WINDOW_FULLSCREEN);
  262. } else {
  263. // Assume we're fullscreen desktop
  264. flags |= SDL_WINDOW_FULLSCREEN;
  265. }
  266. }
  267. if (maximized == 3) {
  268. /* Fullscreen windows are maximized on some window managers,
  269. and this is functional behavior - if maximized is removed,
  270. the windows remain floating centered and not covering the
  271. rest of the desktop. So we just won't change the maximize
  272. state for fullscreen windows here, otherwise SDL would think
  273. we're always maximized when fullscreen and not restore the
  274. correct state when leaving fullscreen.
  275. */
  276. if (fullscreen) {
  277. flags |= (window->flags & SDL_WINDOW_MAXIMIZED);
  278. } else {
  279. flags |= SDL_WINDOW_MAXIMIZED;
  280. }
  281. }
  282. /* If the window is unmapped, numItems will be zero and _NET_WM_STATE_HIDDEN
  283. * will not be set. Do an additional check to see if the window is unmapped
  284. * and mark it as SDL_WINDOW_HIDDEN if it is.
  285. */
  286. {
  287. XWindowAttributes attr;
  288. SDL_memset(&attr, 0, sizeof(attr));
  289. X11_XGetWindowAttributes(videodata->display, xwindow, &attr);
  290. if (attr.map_state == IsUnmapped) {
  291. flags |= SDL_WINDOW_HIDDEN;
  292. }
  293. }
  294. X11_XFree(propertyValue);
  295. }
  296. // FIXME, check the size hints for resizable
  297. // flags |= SDL_WINDOW_RESIZABLE;
  298. return flags;
  299. }
  300. static bool SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, Window w)
  301. {
  302. SDL_VideoData *videodata = _this->internal;
  303. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  304. SDL_WindowData *data;
  305. int numwindows = videodata->numwindows;
  306. int windowlistlength = videodata->windowlistlength;
  307. SDL_WindowData **windowlist = videodata->windowlist;
  308. // Allocate the window data
  309. data = (SDL_WindowData *)SDL_calloc(1, sizeof(*data));
  310. if (!data) {
  311. return false;
  312. }
  313. data->videodata = videodata;
  314. data->window = window;
  315. data->xwindow = w;
  316. data->hit_test_result = SDL_HITTEST_NORMAL;
  317. X11_CreateInputContext(data);
  318. // Associate the data with the window
  319. if (numwindows < windowlistlength) {
  320. windowlist[numwindows] = data;
  321. videodata->numwindows++;
  322. } else {
  323. SDL_WindowData ** new_windowlist = (SDL_WindowData **)SDL_realloc(windowlist, (numwindows + 1) * sizeof(*windowlist));
  324. if (!new_windowlist) {
  325. SDL_free(data);
  326. return false;
  327. }
  328. windowlist = new_windowlist;
  329. windowlist[numwindows] = data;
  330. videodata->numwindows++;
  331. videodata->windowlistlength++;
  332. videodata->windowlist = windowlist;
  333. }
  334. // Fill in the SDL window with the window data
  335. {
  336. XWindowAttributes attrib;
  337. X11_XGetWindowAttributes(data->videodata->display, w, &attrib);
  338. if (!SDL_WINDOW_IS_POPUP(window)) {
  339. window->x = data->expected.x = attrib.x;
  340. window->y = data->expected.y = attrib.y - data->border_top;
  341. }
  342. window->w = data->expected.w = attrib.width;
  343. window->h = data->expected.h = attrib.height;
  344. if (attrib.map_state != IsUnmapped) {
  345. window->flags &= ~SDL_WINDOW_HIDDEN;
  346. } else {
  347. window->flags |= SDL_WINDOW_HIDDEN;
  348. }
  349. data->visual = attrib.visual;
  350. data->colormap = attrib.colormap;
  351. }
  352. window->flags |= X11_GetNetWMState(_this, window, w);
  353. {
  354. Window FocalWindow;
  355. int RevertTo = 0;
  356. X11_XGetInputFocus(data->videodata->display, &FocalWindow, &RevertTo);
  357. if (FocalWindow == w) {
  358. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  359. }
  360. if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  361. SDL_SetKeyboardFocus(data->window);
  362. }
  363. if (window->flags & SDL_WINDOW_MOUSE_GRABBED) {
  364. // Tell x11 to clip mouse
  365. }
  366. }
  367. if (window->flags & SDL_WINDOW_EXTERNAL) {
  368. // Query the title from the existing window
  369. window->title = X11_GetWindowTitle(_this, w);
  370. }
  371. SDL_PropertiesID props = SDL_GetWindowProperties(window);
  372. int screen = (displaydata ? displaydata->screen : 0);
  373. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, data->videodata->display);
  374. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_X11_SCREEN_NUMBER, screen);
  375. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, data->xwindow);
  376. // All done!
  377. window->internal = data;
  378. return true;
  379. }
  380. static void SetupWindowInput(SDL_VideoDevice *_this, SDL_Window *window)
  381. {
  382. long fevent = 0;
  383. SDL_WindowData *data = window->internal;
  384. Window xwindow = data->xwindow;
  385. #ifdef X_HAVE_UTF8_STRING
  386. if (SDL_X11_HAVE_UTF8 && data->ic) {
  387. X11_XGetICValues(data->ic, XNFilterEvents, &fevent, NULL);
  388. }
  389. #endif
  390. X11_Xinput2SelectTouch(_this, window);
  391. {
  392. unsigned int x11_keyboard_events = KeyPressMask | KeyReleaseMask;
  393. unsigned int x11_pointer_events = ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
  394. X11_Xinput2SelectMouseAndKeyboard(_this, window);
  395. // If XInput2 can handle pointer and keyboard events, we don't track them here
  396. if (data->xinput2_keyboard_enabled) {
  397. x11_keyboard_events = 0;
  398. }
  399. if (data->xinput2_mouse_enabled) {
  400. x11_pointer_events = 0;
  401. }
  402. X11_XSelectInput(data->videodata->display, xwindow,
  403. (FocusChangeMask | EnterWindowMask | LeaveWindowMask | ExposureMask |
  404. x11_keyboard_events | x11_pointer_events |
  405. PropertyChangeMask | StructureNotifyMask |
  406. KeymapStateMask | fevent));
  407. }
  408. }
  409. static void SetWindowBordered(Display *display, int screen, Window window, bool border)
  410. {
  411. /*
  412. * this code used to check for KWM_WIN_DECORATION, but KDE hasn't
  413. * supported it for years and years. It now respects _MOTIF_WM_HINTS.
  414. * Gnome is similar: just use the Motif atom.
  415. */
  416. Atom WM_HINTS = X11_XInternAtom(display, "_MOTIF_WM_HINTS", True);
  417. if (WM_HINTS != None) {
  418. // Hints used by Motif compliant window managers
  419. struct
  420. {
  421. unsigned long flags;
  422. unsigned long functions;
  423. unsigned long decorations;
  424. long input_mode;
  425. unsigned long status;
  426. } MWMHints = {
  427. (1L << 1), 0, border ? 1 : 0, 0, 0
  428. };
  429. X11_XChangeProperty(display, window, WM_HINTS, WM_HINTS, 32,
  430. PropModeReplace, (unsigned char *)&MWMHints,
  431. sizeof(MWMHints) / sizeof(long));
  432. } else { // set the transient hints instead, if necessary
  433. X11_XSetTransientForHint(display, window, RootWindow(display, screen));
  434. }
  435. }
  436. bool X11_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props)
  437. {
  438. Window w = (Window)SDL_GetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_X11_WINDOW_NUMBER,
  439. (Window)SDL_GetPointerProperty(create_props, "sdl2-compat.external_window", NULL));
  440. if (w) {
  441. window->flags |= SDL_WINDOW_EXTERNAL;
  442. if (!SetupWindowData(_this, window, w)) {
  443. return false;
  444. }
  445. SetupWindowInput(_this, window);
  446. return true;
  447. }
  448. SDL_VideoData *data = _this->internal;
  449. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  450. if (!displaydata) {
  451. return SDL_SetError("Could not find display info");
  452. }
  453. const bool force_override_redirect = SDL_GetHintBoolean(SDL_HINT_X11_FORCE_OVERRIDE_REDIRECT, false);
  454. const bool use_resize_sync = (window->flags & SDL_WINDOW_VULKAN); /* doesn't work well with Vulkan */
  455. SDL_WindowData *windowdata;
  456. Display *display = data->display;
  457. int screen = displaydata->screen;
  458. Visual *visual;
  459. int depth;
  460. XSetWindowAttributes xattr;
  461. XSizeHints *sizehints;
  462. XWMHints *wmhints;
  463. XClassHint *classhints;
  464. Atom _NET_WM_BYPASS_COMPOSITOR;
  465. Atom _NET_WM_WINDOW_TYPE;
  466. Atom wintype;
  467. const char *wintype_name = NULL;
  468. long compositor = 1;
  469. Atom _NET_WM_PID;
  470. const char *hint = NULL;
  471. int win_x, win_y;
  472. bool undefined_position = false;
  473. #if defined(SDL_VIDEO_OPENGL_GLX) || defined(SDL_VIDEO_OPENGL_EGL)
  474. const int transparent = (window->flags & SDL_WINDOW_TRANSPARENT) ? true : false;
  475. const char *forced_visual_id = SDL_GetHint(SDL_HINT_VIDEO_X11_WINDOW_VISUALID);
  476. const char *display_visual_id = SDL_GetHint(SDL_HINT_VIDEO_X11_VISUALID);
  477. if (forced_visual_id && *forced_visual_id) {
  478. XVisualInfo *vi, template;
  479. int nvis;
  480. SDL_zero(template);
  481. template.visualid = SDL_strtol(forced_visual_id, NULL, 0);
  482. vi = X11_XGetVisualInfo(display, VisualIDMask, &template, &nvis);
  483. if (vi) {
  484. visual = vi->visual;
  485. depth = vi->depth;
  486. X11_XFree(vi);
  487. } else {
  488. return false;
  489. }
  490. } else if ((window->flags & SDL_WINDOW_OPENGL) &&
  491. (!display_visual_id || !*display_visual_id)) {
  492. XVisualInfo *vinfo = NULL;
  493. #ifdef SDL_VIDEO_OPENGL_EGL
  494. if (((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) ||
  495. SDL_GetHintBoolean(SDL_HINT_VIDEO_FORCE_EGL, false))
  496. #ifdef SDL_VIDEO_OPENGL_GLX
  497. && (!_this->gl_data || X11_GL_UseEGL(_this))
  498. #endif
  499. ) {
  500. vinfo = X11_GLES_GetVisual(_this, display, screen, transparent);
  501. } else
  502. #endif
  503. {
  504. #ifdef SDL_VIDEO_OPENGL_GLX
  505. vinfo = X11_GL_GetVisual(_this, display, screen, transparent);
  506. #endif
  507. }
  508. if (!vinfo) {
  509. return false;
  510. }
  511. visual = vinfo->visual;
  512. depth = vinfo->depth;
  513. X11_XFree(vinfo);
  514. } else
  515. #endif
  516. {
  517. visual = displaydata->visual;
  518. depth = displaydata->depth;
  519. }
  520. xattr.override_redirect = ((window->flags & SDL_WINDOW_TOOLTIP) || (window->flags & SDL_WINDOW_POPUP_MENU) || force_override_redirect) ? True : False;
  521. xattr.backing_store = NotUseful;
  522. xattr.background_pixmap = None;
  523. xattr.border_pixel = 0;
  524. if (visual->class == DirectColor) {
  525. XColor *colorcells;
  526. int i;
  527. int ncolors;
  528. int rmax, gmax, bmax;
  529. int rmask, gmask, bmask;
  530. int rshift, gshift, bshift;
  531. xattr.colormap =
  532. X11_XCreateColormap(display, RootWindow(display, screen),
  533. visual, AllocAll);
  534. // If we can't create a colormap, then we must die
  535. if (!xattr.colormap) {
  536. return SDL_SetError("Could not create writable colormap");
  537. }
  538. // OK, we got a colormap, now fill it in as best as we can
  539. colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
  540. if (!colorcells) {
  541. return false;
  542. }
  543. ncolors = visual->map_entries;
  544. rmax = 0xffff;
  545. gmax = 0xffff;
  546. bmax = 0xffff;
  547. rshift = 0;
  548. rmask = visual->red_mask;
  549. while (0 == (rmask & 1)) {
  550. rshift++;
  551. rmask >>= 1;
  552. }
  553. gshift = 0;
  554. gmask = visual->green_mask;
  555. while (0 == (gmask & 1)) {
  556. gshift++;
  557. gmask >>= 1;
  558. }
  559. bshift = 0;
  560. bmask = visual->blue_mask;
  561. while (0 == (bmask & 1)) {
  562. bshift++;
  563. bmask >>= 1;
  564. }
  565. // build the color table pixel values
  566. for (i = 0; i < ncolors; i++) {
  567. Uint32 red = (rmax * i) / (ncolors - 1);
  568. Uint32 green = (gmax * i) / (ncolors - 1);
  569. Uint32 blue = (bmax * i) / (ncolors - 1);
  570. Uint32 rbits = (rmask * i) / (ncolors - 1);
  571. Uint32 gbits = (gmask * i) / (ncolors - 1);
  572. Uint32 bbits = (bmask * i) / (ncolors - 1);
  573. Uint32 pix =
  574. (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
  575. colorcells[i].pixel = pix;
  576. colorcells[i].red = red;
  577. colorcells[i].green = green;
  578. colorcells[i].blue = blue;
  579. colorcells[i].flags = DoRed | DoGreen | DoBlue;
  580. }
  581. X11_XStoreColors(display, xattr.colormap, colorcells, ncolors);
  582. SDL_free(colorcells);
  583. } else {
  584. xattr.colormap =
  585. X11_XCreateColormap(display, RootWindow(display, screen),
  586. visual, AllocNone);
  587. }
  588. if (window->undefined_x && window->undefined_y &&
  589. window->last_displayID == SDL_GetPrimaryDisplay()) {
  590. undefined_position = true;
  591. }
  592. if (SDL_WINDOW_IS_POPUP(window)) {
  593. X11_ConstrainPopup(window, false);
  594. }
  595. SDL_RelativeToGlobalForWindow(window,
  596. window->floating.x, window->floating.y,
  597. &win_x, &win_y);
  598. /* Always create this with the window->floating.* fields; if we're creating a windowed mode window,
  599. * that's fine. If we're creating a maximized or fullscreen window, the window manager will want to
  600. * know these values so it can use them if we go _back_ to the base floating windowed mode. SDL manages
  601. * migration to fullscreen after CreateSDLWindow returns, which will put all the SDL_Window fields and
  602. * system state as expected.
  603. */
  604. w = X11_XCreateWindow(display, RootWindow(display, screen),
  605. win_x, win_y, window->floating.w, window->floating.h,
  606. 0, depth, InputOutput, visual,
  607. (CWOverrideRedirect | CWBackPixmap | CWBorderPixel |
  608. CWBackingStore | CWColormap),
  609. &xattr);
  610. if (!w) {
  611. return SDL_SetError("Couldn't create window");
  612. }
  613. /* Don't set the borderless flag if we're about to go fullscreen.
  614. * This prevents the window manager from moving a full-screen borderless
  615. * window to a different display before we actually go fullscreen.
  616. */
  617. if (!(window->pending_flags & SDL_WINDOW_FULLSCREEN)) {
  618. SetWindowBordered(display, screen, w, !(window->flags & SDL_WINDOW_BORDERLESS));
  619. }
  620. sizehints = X11_XAllocSizeHints();
  621. // Setup the normal size hints
  622. sizehints->flags = 0;
  623. if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
  624. sizehints->min_width = sizehints->max_width = window->floating.w;
  625. sizehints->min_height = sizehints->max_height = window->floating.h;
  626. sizehints->flags |= (PMaxSize | PMinSize);
  627. }
  628. if (!undefined_position) {
  629. sizehints->x = win_x;
  630. sizehints->y = win_y;
  631. sizehints->flags |= USPosition;
  632. }
  633. // Setup the input hints so we get keyboard input
  634. wmhints = X11_XAllocWMHints();
  635. wmhints->input = !(window->flags & SDL_WINDOW_NOT_FOCUSABLE) ? True : False;
  636. wmhints->window_group = data->window_group;
  637. wmhints->flags = InputHint | WindowGroupHint;
  638. // Setup the class hints so we can get an icon (AfterStep)
  639. classhints = X11_XAllocClassHint();
  640. classhints->res_name = (char *)SDL_GetExeName();
  641. classhints->res_class = (char *)SDL_GetAppID();
  642. // Set the size, input and class hints, and define WM_CLIENT_MACHINE and WM_LOCALE_NAME
  643. X11_XSetWMProperties(display, w, NULL, NULL, NULL, 0, sizehints, wmhints, classhints);
  644. X11_XFree(sizehints);
  645. X11_XFree(wmhints);
  646. X11_XFree(classhints);
  647. // Set the PID related to the window for the given hostname, if possible
  648. if (data->pid > 0) {
  649. long pid = (long)data->pid;
  650. _NET_WM_PID = X11_XInternAtom(display, "_NET_WM_PID", False);
  651. X11_XChangeProperty(display, w, _NET_WM_PID, XA_CARDINAL, 32, PropModeReplace,
  652. (unsigned char *)&pid, 1);
  653. }
  654. // Set the window manager state
  655. X11_SetNetWMState(_this, w, window->flags);
  656. compositor = 2; // don't disable compositing except for "normal" windows
  657. hint = SDL_GetHint(SDL_HINT_X11_WINDOW_TYPE);
  658. if (window->flags & SDL_WINDOW_UTILITY) {
  659. wintype_name = "_NET_WM_WINDOW_TYPE_UTILITY";
  660. } else if (window->flags & SDL_WINDOW_TOOLTIP) {
  661. wintype_name = "_NET_WM_WINDOW_TYPE_TOOLTIP";
  662. } else if (window->flags & SDL_WINDOW_POPUP_MENU) {
  663. wintype_name = "_NET_WM_WINDOW_TYPE_POPUP_MENU";
  664. } else if (hint && *hint) {
  665. wintype_name = hint;
  666. } else {
  667. wintype_name = "_NET_WM_WINDOW_TYPE_NORMAL";
  668. compositor = 1; // disable compositing for "normal" windows
  669. }
  670. // Let the window manager know what type of window we are.
  671. _NET_WM_WINDOW_TYPE = X11_XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
  672. wintype = X11_XInternAtom(display, wintype_name, False);
  673. X11_XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
  674. PropModeReplace, (unsigned char *)&wintype, 1);
  675. if (SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, true)) {
  676. _NET_WM_BYPASS_COMPOSITOR = X11_XInternAtom(display, "_NET_WM_BYPASS_COMPOSITOR", False);
  677. X11_XChangeProperty(display, w, _NET_WM_BYPASS_COMPOSITOR, XA_CARDINAL, 32,
  678. PropModeReplace,
  679. (unsigned char *)&compositor, 1);
  680. }
  681. {
  682. Atom protocols[4];
  683. int proto_count = 0;
  684. protocols[proto_count++] = data->atoms.WM_DELETE_WINDOW; // Allow window to be deleted by the WM
  685. protocols[proto_count++] = data->atoms.WM_TAKE_FOCUS; // Since we will want to set input focus explicitly
  686. // Default to using ping if there is no hint
  687. if (SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_NET_WM_PING, true)) {
  688. protocols[proto_count++] = data->atoms._NET_WM_PING; // Respond so WM knows we're alive
  689. }
  690. #ifdef SDL_VIDEO_DRIVER_X11_XSYNC
  691. if (use_resize_sync) {
  692. protocols[proto_count++] = data->atoms._NET_WM_SYNC_REQUEST; /* Respond after completing resize */
  693. }
  694. #endif /* SDL_VIDEO_DRIVER_X11_XSYNC */
  695. SDL_assert(proto_count <= sizeof(protocols) / sizeof(protocols[0]));
  696. X11_XSetWMProtocols(display, w, protocols, proto_count);
  697. }
  698. if (!SetupWindowData(_this, window, w)) {
  699. X11_XDestroyWindow(display, w);
  700. return false;
  701. }
  702. windowdata = window->internal;
  703. // Set the parent if this is a non-popup window.
  704. if (!SDL_WINDOW_IS_POPUP(window) && window->parent) {
  705. X11_XSetTransientForHint(display, w, window->parent->internal->xwindow);
  706. }
  707. // Set the flag if the borders were forced on when creating a fullscreen window for later removal.
  708. windowdata->fullscreen_borders_forced_on = !!(window->pending_flags & SDL_WINDOW_FULLSCREEN) &&
  709. !!(window->flags & SDL_WINDOW_BORDERLESS);
  710. #ifdef SDL_VIDEO_DRIVER_X11_XSYNC
  711. if (use_resize_sync) {
  712. X11_InitResizeSync(window);
  713. }
  714. #endif /* SDL_VIDEO_DRIVER_X11_XSYNC */
  715. #if defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2) || defined(SDL_VIDEO_OPENGL_EGL)
  716. if ((window->flags & SDL_WINDOW_OPENGL) &&
  717. ((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) ||
  718. SDL_GetHintBoolean(SDL_HINT_VIDEO_FORCE_EGL, false))
  719. #ifdef SDL_VIDEO_OPENGL_GLX
  720. && (!_this->gl_data || X11_GL_UseEGL(_this))
  721. #endif
  722. ) {
  723. #ifdef SDL_VIDEO_OPENGL_EGL
  724. if (!_this->egl_data) {
  725. return false;
  726. }
  727. // Create the GLES window surface
  728. windowdata->egl_surface = SDL_EGL_CreateSurface(_this, window, (NativeWindowType)w);
  729. if (windowdata->egl_surface == EGL_NO_SURFACE) {
  730. return SDL_SetError("Could not create GLES window surface");
  731. }
  732. #else
  733. return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
  734. #endif // SDL_VIDEO_OPENGL_EGL
  735. }
  736. #endif
  737. #ifdef SDL_VIDEO_DRIVER_X11_XSHAPE
  738. // Tooltips do not receive input
  739. if (window->flags & SDL_WINDOW_TOOLTIP) {
  740. Region region = X11_XCreateRegion();
  741. X11_XShapeCombineRegion(display, w, ShapeInput, 0, 0, region, ShapeSet);
  742. X11_XDestroyRegion(region);
  743. }
  744. #endif
  745. SetupWindowInput(_this, window);
  746. // For _ICC_PROFILE.
  747. X11_XSelectInput(display, RootWindow(display, screen), PropertyChangeMask);
  748. X11_XFlush(display);
  749. return true;
  750. }
  751. char *X11_GetWindowTitle(SDL_VideoDevice *_this, Window xwindow)
  752. {
  753. SDL_VideoData *data = _this->internal;
  754. Display *display = data->display;
  755. int status, real_format;
  756. Atom real_type;
  757. unsigned long items_read, items_left;
  758. unsigned char *propdata;
  759. char *title = NULL;
  760. status = X11_XGetWindowProperty(display, xwindow, data->atoms._NET_WM_NAME,
  761. 0L, 8192L, False, data->atoms.UTF8_STRING, &real_type, &real_format,
  762. &items_read, &items_left, &propdata);
  763. if (status == Success && propdata) {
  764. title = SDL_strdup(SDL_static_cast(char *, propdata));
  765. X11_XFree(propdata);
  766. } else {
  767. status = X11_XGetWindowProperty(display, xwindow, XA_WM_NAME,
  768. 0L, 8192L, False, XA_STRING, &real_type, &real_format,
  769. &items_read, &items_left, &propdata);
  770. if (status == Success && propdata) {
  771. title = SDL_iconv_string("UTF-8", "", SDL_static_cast(char *, propdata), items_read + 1);
  772. SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Failed to convert WM_NAME title expecting UTF8! Title: %s", title);
  773. X11_XFree(propdata);
  774. } else {
  775. SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Could not get any window title response from Xorg, returning empty string!");
  776. title = SDL_strdup("");
  777. }
  778. }
  779. return title;
  780. }
  781. void X11_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window)
  782. {
  783. SDL_WindowData *data = window->internal;
  784. Window xwindow = data->xwindow;
  785. Display *display = data->videodata->display;
  786. char *title = window->title ? window->title : "";
  787. SDL_X11_SetWindowTitle(display, xwindow, title);
  788. }
  789. static bool caught_x11_error = false;
  790. static int X11_CatchAnyError(Display *d, XErrorEvent *e)
  791. {
  792. /* this may happen during tumultuous times when we are polling anyhow,
  793. so just note we had an error and return control. */
  794. caught_x11_error = true;
  795. return 0;
  796. }
  797. /* Wait a brief time, or not, to see if the window manager decided to move/resize the window.
  798. * Send MOVED and RESIZED window events */
  799. static bool X11_SyncWindowTimeout(SDL_VideoDevice *_this, SDL_Window *window, Uint64 param_timeout)
  800. {
  801. SDL_WindowData *data = window->internal;
  802. Display *display = data->videodata->display;
  803. int (*prev_handler)(Display *, XErrorEvent *);
  804. Uint64 timeout = 0;
  805. bool force_exit = false;
  806. bool result = true;
  807. X11_XSync(display, False);
  808. prev_handler = X11_XSetErrorHandler(X11_CatchAnyError);
  809. if (param_timeout) {
  810. timeout = SDL_GetTicksNS() + param_timeout;
  811. }
  812. while (true) {
  813. X11_XSync(display, False);
  814. X11_PumpEvents(_this);
  815. if ((data->pending_operation & X11_PENDING_OP_MOVE) && (window->x == data->expected.x + data->border_left && window->y == data->expected.y + data->border_top)) {
  816. data->pending_operation &= ~X11_PENDING_OP_MOVE;
  817. }
  818. if ((data->pending_operation & X11_PENDING_OP_RESIZE) && (window->w == data->expected.w && window->h == data->expected.h)) {
  819. data->pending_operation &= ~X11_PENDING_OP_RESIZE;
  820. }
  821. if (data->pending_operation == X11_PENDING_OP_NONE) {
  822. if (force_exit ||
  823. (window->x == data->expected.x + data->border_left && window->y == data->expected.y + data->border_top &&
  824. window->w == data->expected.w && window->h == data->expected.h)) {
  825. // The window is in the expected state and nothing is pending. Done.
  826. break;
  827. }
  828. /* No operations are pending, but the window still isn't in the expected state.
  829. * Try one more time before exiting.
  830. */
  831. force_exit = true;
  832. }
  833. if (SDL_GetTicksNS() >= timeout) {
  834. // Timed out without the expected values. Update the requested data so future sync calls won't block.
  835. data->expected.x = window->x;
  836. data->expected.y = window->y;
  837. data->expected.w = window->w;
  838. data->expected.h = window->h;
  839. result = false;
  840. break;
  841. }
  842. SDL_Delay(10);
  843. }
  844. data->pending_operation = X11_PENDING_OP_NONE;
  845. if (!caught_x11_error) {
  846. X11_PumpEvents(_this);
  847. } else {
  848. result = false;
  849. }
  850. X11_XSetErrorHandler(prev_handler);
  851. caught_x11_error = false;
  852. return result;
  853. }
  854. bool X11_SetWindowIcon(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surface *icon)
  855. {
  856. SDL_WindowData *data = window->internal;
  857. Display *display = data->videodata->display;
  858. Atom _NET_WM_ICON = data->videodata->atoms._NET_WM_ICON;
  859. int (*prevHandler)(Display *, XErrorEvent *) = NULL;
  860. bool result = true;
  861. if (icon) {
  862. int x, y;
  863. int propsize;
  864. long *propdata;
  865. Uint32 *src;
  866. long *dst;
  867. // Set the _NET_WM_ICON property
  868. SDL_assert(icon->format == SDL_PIXELFORMAT_ARGB8888);
  869. propsize = 2 + (icon->w * icon->h);
  870. propdata = SDL_malloc(propsize * sizeof(long));
  871. if (!propdata) {
  872. return false;
  873. }
  874. X11_XSync(display, False);
  875. prevHandler = X11_XSetErrorHandler(X11_CatchAnyError);
  876. propdata[0] = icon->w;
  877. propdata[1] = icon->h;
  878. dst = &propdata[2];
  879. for (y = 0; y < icon->h; ++y) {
  880. src = (Uint32 *)((Uint8 *)icon->pixels + y * icon->pitch);
  881. for (x = 0; x < icon->w; ++x) {
  882. *dst++ = *src++;
  883. }
  884. }
  885. X11_XChangeProperty(display, data->xwindow, _NET_WM_ICON, XA_CARDINAL,
  886. 32, PropModeReplace, (unsigned char *)propdata,
  887. propsize);
  888. SDL_free(propdata);
  889. if (caught_x11_error) {
  890. result = SDL_SetError("An error occurred while trying to set the window's icon");
  891. }
  892. }
  893. X11_XFlush(display);
  894. if (prevHandler) {
  895. X11_XSetErrorHandler(prevHandler);
  896. caught_x11_error = false;
  897. }
  898. return result;
  899. }
  900. void X11_UpdateWindowPosition(SDL_Window *window, bool use_current_position)
  901. {
  902. SDL_WindowData *data = window->internal;
  903. Display *display = data->videodata->display;
  904. const int rel_x = use_current_position ? window->x : window->pending.x;
  905. const int rel_y = use_current_position ? window->y : window->pending.y;
  906. SDL_RelativeToGlobalForWindow(window,
  907. rel_x - data->border_left, rel_y - data->border_top,
  908. &data->expected.x, &data->expected.y);
  909. // Attempt to move the window
  910. data->pending_operation |= X11_PENDING_OP_MOVE;
  911. X11_XMoveWindow(display, data->xwindow, data->expected.x, data->expected.y);
  912. }
  913. bool X11_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window)
  914. {
  915. // Sync any pending fullscreen or maximize events.
  916. if (window->internal->pending_operation & (X11_PENDING_OP_FULLSCREEN | X11_PENDING_OP_MAXIMIZE)) {
  917. X11_FlushPendingEvents(_this, window);
  918. }
  919. // Set the position as pending if the window is maximized with a restore pending.
  920. if (window->flags & SDL_WINDOW_MAXIMIZED) {
  921. if (window->internal->pending_operation & X11_PENDING_OP_RESTORE) {
  922. window->internal->pending_position = true;
  923. }
  924. return true;
  925. }
  926. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  927. if (SDL_WINDOW_IS_POPUP(window)) {
  928. X11_ConstrainPopup(window, true);
  929. }
  930. X11_UpdateWindowPosition(window, false);
  931. } else {
  932. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_UPDATE, true);
  933. }
  934. return true;
  935. }
  936. static void X11_SetWMNormalHints(SDL_VideoDevice *_this, SDL_Window *window, XSizeHints *sizehints)
  937. {
  938. SDL_WindowData *data = window->internal;
  939. Display *display = data->videodata->display;
  940. int dest_x, dest_y;
  941. X11_XSetWMNormalHints(display, data->xwindow, sizehints);
  942. /* From Pierre-Loup:
  943. WMs each have their little quirks with that. When you change the
  944. size hints, they get a ConfigureNotify event with the
  945. WM_NORMAL_SIZE_HINTS Atom. They all save the hints then, but they
  946. don't all resize the window right away to enforce the new hints.
  947. Some of them resize only after:
  948. - A user-initiated move or resize
  949. - A code-initiated move or resize
  950. - Hiding & showing window (Unmap & map)
  951. The following move & resize seems to help a lot of WMs that didn't
  952. properly update after the hints were changed. We don't do a
  953. hide/show, because there are supposedly subtle problems with doing so
  954. and transitioning from windowed to fullscreen in Unity.
  955. */
  956. X11_XResizeWindow(display, data->xwindow, window->pending.w, window->pending.h);
  957. const int x = window->last_position_pending ? window->pending.x : window->floating.x;
  958. const int y = window->last_position_pending ? window->pending.y : window->floating.y;
  959. SDL_RelativeToGlobalForWindow(window,
  960. x - data->border_left,
  961. y - data->border_top,
  962. &dest_x, &dest_y);
  963. X11_XMoveWindow(display, data->xwindow, dest_x, dest_y);
  964. X11_XRaiseWindow(display, data->xwindow);
  965. }
  966. void X11_SetWindowMinMax(SDL_Window *window, bool use_current)
  967. {
  968. SDL_WindowData *data = window->internal;
  969. Display *display = data->videodata->display;
  970. XSizeHints *sizehints = X11_XAllocSizeHints();
  971. long hint_flags = 0;
  972. X11_XGetWMNormalHints(display, data->xwindow, sizehints, &hint_flags);
  973. sizehints->flags &= ~(PMinSize | PMaxSize | PAspect);
  974. if (data->window->flags & SDL_WINDOW_RESIZABLE) {
  975. if (data->window->min_w || data->window->min_h) {
  976. sizehints->flags |= PMinSize;
  977. sizehints->min_width = data->window->min_w;
  978. sizehints->min_height = data->window->min_h;
  979. }
  980. if (data->window->max_w || data->window->max_h) {
  981. sizehints->flags |= PMaxSize;
  982. sizehints->max_width = data->window->max_w;
  983. sizehints->max_height = data->window->max_h;
  984. }
  985. if (data->window->min_aspect > 0.0f || data->window->max_aspect > 0.0f) {
  986. sizehints->flags |= PAspect;
  987. SDL_CalculateFraction(data->window->min_aspect, &sizehints->min_aspect.x, &sizehints->min_aspect.y);
  988. SDL_CalculateFraction(data->window->max_aspect, &sizehints->max_aspect.x, &sizehints->max_aspect.y);
  989. }
  990. } else {
  991. // Set the min/max to the same values to make the window non-resizable
  992. sizehints->flags |= PMinSize | PMaxSize;
  993. sizehints->min_width = sizehints->max_width = use_current ? data->window->floating.w : window->windowed.w;
  994. sizehints->min_height = sizehints->max_height = use_current ? data->window->floating.h : window->windowed.h;
  995. }
  996. X11_XSetWMNormalHints(display, data->xwindow, sizehints);
  997. X11_XFree(sizehints);
  998. }
  999. void X11_SetWindowMinimumSize(SDL_VideoDevice *_this, SDL_Window *window)
  1000. {
  1001. if (window->internal->pending_operation & X11_PENDING_OP_FULLSCREEN) {
  1002. X11_SyncWindow(_this, window);
  1003. }
  1004. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1005. X11_SetWindowMinMax(window, true);
  1006. }
  1007. }
  1008. void X11_SetWindowMaximumSize(SDL_VideoDevice *_this, SDL_Window *window)
  1009. {
  1010. if (window->internal->pending_operation & X11_PENDING_OP_FULLSCREEN) {
  1011. X11_SyncWindow(_this, window);
  1012. }
  1013. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1014. X11_SetWindowMinMax(window, true);
  1015. }
  1016. }
  1017. void X11_SetWindowAspectRatio(SDL_VideoDevice *_this, SDL_Window *window)
  1018. {
  1019. if (window->internal->pending_operation & X11_PENDING_OP_FULLSCREEN) {
  1020. X11_SyncWindow(_this, window);
  1021. }
  1022. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1023. X11_SetWindowMinMax(window, true);
  1024. }
  1025. }
  1026. void X11_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
  1027. {
  1028. SDL_WindowData *data = window->internal;
  1029. Display *display = data->videodata->display;
  1030. /* Wait for pending maximize and fullscreen operations to complete, as these windows
  1031. * don't get size changes.
  1032. */
  1033. if (data->pending_operation & (X11_PENDING_OP_MAXIMIZE | X11_PENDING_OP_FULLSCREEN)) {
  1034. X11_FlushPendingEvents(_this, window);
  1035. }
  1036. // Set the size as pending if the window is being restored.
  1037. if (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_FULLSCREEN)) {
  1038. // New size will be set when the window is restored.
  1039. if (data->pending_operation & X11_PENDING_OP_RESTORE) {
  1040. data->pending_size = true;
  1041. } else {
  1042. // Can't resize the window.
  1043. window->last_size_pending = false;
  1044. }
  1045. return;
  1046. }
  1047. if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
  1048. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1049. /* Apparently, if the X11 Window is set to a 'non-resizable' window, you cannot resize it using the X11_XResizeWindow, thus
  1050. * we must set the size hints to adjust the window size.
  1051. */
  1052. XSizeHints *sizehints = X11_XAllocSizeHints();
  1053. long userhints;
  1054. X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
  1055. sizehints->min_width = sizehints->max_width = window->pending.w;
  1056. sizehints->min_height = sizehints->max_height = window->pending.h;
  1057. sizehints->flags |= PMinSize | PMaxSize;
  1058. X11_SetWMNormalHints(_this, window, sizehints);
  1059. X11_XFree(sizehints);
  1060. }
  1061. } else {
  1062. data->expected.w = window->pending.w;
  1063. data->expected.h = window->pending.h;
  1064. data->pending_operation |= X11_PENDING_OP_RESIZE;
  1065. X11_XResizeWindow(display, data->xwindow, data->expected.w, data->expected.h);
  1066. }
  1067. }
  1068. bool X11_GetWindowBordersSize(SDL_VideoDevice *_this, SDL_Window *window, int *top, int *left, int *bottom, int *right)
  1069. {
  1070. SDL_WindowData *data = window->internal;
  1071. *left = data->border_left;
  1072. *right = data->border_right;
  1073. *top = data->border_top;
  1074. *bottom = data->border_bottom;
  1075. return true;
  1076. }
  1077. bool X11_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float opacity)
  1078. {
  1079. SDL_WindowData *data = window->internal;
  1080. Display *display = data->videodata->display;
  1081. Atom _NET_WM_WINDOW_OPACITY = data->videodata->atoms._NET_WM_WINDOW_OPACITY;
  1082. if (opacity == 1.0f) {
  1083. X11_XDeleteProperty(display, data->xwindow, _NET_WM_WINDOW_OPACITY);
  1084. } else {
  1085. const Uint32 FullyOpaque = 0xFFFFFFFF;
  1086. const long alpha = (long)((double)opacity * (double)FullyOpaque);
  1087. X11_XChangeProperty(display, data->xwindow, _NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32,
  1088. PropModeReplace, (unsigned char *)&alpha, 1);
  1089. }
  1090. return true;
  1091. }
  1092. bool X11_SetWindowParent(SDL_VideoDevice *_this, SDL_Window *window, SDL_Window *parent)
  1093. {
  1094. SDL_WindowData *data = window->internal;
  1095. SDL_WindowData *parent_data = parent ? parent->internal : NULL;
  1096. SDL_VideoData *video_data = _this->internal;
  1097. Display *display = video_data->display;
  1098. if (parent_data) {
  1099. X11_XSetTransientForHint(display, data->xwindow, parent_data->xwindow);
  1100. } else {
  1101. X11_XDeleteProperty(display, data->xwindow, video_data->atoms.WM_TRANSIENT_FOR);
  1102. }
  1103. return true;
  1104. }
  1105. bool X11_SetWindowModal(SDL_VideoDevice *_this, SDL_Window *window, bool modal)
  1106. {
  1107. SDL_WindowData *data = window->internal;
  1108. SDL_VideoData *video_data = _this->internal;
  1109. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  1110. Display *display = video_data->display;
  1111. Uint32 flags = window->flags;
  1112. Atom _NET_WM_STATE = data->videodata->atoms._NET_WM_STATE;
  1113. Atom _NET_WM_STATE_MODAL = data->videodata->atoms._NET_WM_STATE_MODAL;
  1114. if (modal) {
  1115. flags |= SDL_WINDOW_MODAL;
  1116. } else {
  1117. flags &= ~SDL_WINDOW_MODAL;
  1118. X11_XDeleteProperty(display, data->xwindow, video_data->atoms.WM_TRANSIENT_FOR);
  1119. }
  1120. if (X11_IsWindowMapped(_this, window)) {
  1121. XEvent e;
  1122. SDL_zero(e);
  1123. e.xany.type = ClientMessage;
  1124. e.xclient.message_type = _NET_WM_STATE;
  1125. e.xclient.format = 32;
  1126. e.xclient.window = data->xwindow;
  1127. e.xclient.data.l[0] = modal ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  1128. e.xclient.data.l[1] = _NET_WM_STATE_MODAL;
  1129. e.xclient.data.l[3] = 0l;
  1130. X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
  1131. SubstructureNotifyMask | SubstructureRedirectMask, &e);
  1132. } else {
  1133. X11_SetNetWMState(_this, data->xwindow, flags);
  1134. }
  1135. X11_XFlush(display);
  1136. return true;
  1137. }
  1138. void X11_SetWindowBordered(SDL_VideoDevice *_this, SDL_Window *window, bool bordered)
  1139. {
  1140. const bool focused = (window->flags & SDL_WINDOW_INPUT_FOCUS) ? true : false;
  1141. const bool visible = (!(window->flags & SDL_WINDOW_HIDDEN)) ? true : false;
  1142. SDL_WindowData *data = window->internal;
  1143. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  1144. Display *display = data->videodata->display;
  1145. XEvent event;
  1146. if (data->pending_operation & X11_PENDING_OP_FULLSCREEN) {
  1147. X11_SyncWindow(_this, window);
  1148. }
  1149. // If the window is fullscreen, the resize capability will be set/cleared when it is returned to windowed mode.
  1150. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1151. SetWindowBordered(display, displaydata->screen, data->xwindow, bordered);
  1152. X11_XFlush(display);
  1153. if (visible) {
  1154. XWindowAttributes attr;
  1155. do {
  1156. X11_XSync(display, False);
  1157. X11_XGetWindowAttributes(display, data->xwindow, &attr);
  1158. } while (attr.map_state != IsViewable);
  1159. if (focused) {
  1160. X11_XSetInputFocus(display, data->xwindow, RevertToParent, CurrentTime);
  1161. }
  1162. }
  1163. // make sure these don't make it to the real event queue if they fired here.
  1164. X11_XSync(display, False);
  1165. X11_XCheckIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
  1166. X11_XCheckIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
  1167. // Turning the borders off doesn't send an extent event, so they must be cleared here.
  1168. X11_GetBorderValues(data);
  1169. // Make sure the window manager didn't resize our window for the difference.
  1170. X11_XResizeWindow(display, data->xwindow, window->floating.w, window->floating.h);
  1171. X11_XSync(display, False);
  1172. } else {
  1173. // If fullscreen, set a flag to toggle the borders when returning to windowed mode.
  1174. data->toggle_borders = true;
  1175. data->fullscreen_borders_forced_on = false;
  1176. }
  1177. }
  1178. void X11_SetWindowResizable(SDL_VideoDevice *_this, SDL_Window *window, bool resizable)
  1179. {
  1180. SDL_WindowData *data = window->internal;
  1181. if (data->pending_operation & X11_PENDING_OP_FULLSCREEN) {
  1182. X11_SyncWindow(_this, window);
  1183. }
  1184. // If the window is fullscreen, the resize capability will be set/cleared when it is returned to windowed mode.
  1185. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1186. X11_SetWindowMinMax(window, true);
  1187. }
  1188. }
  1189. void X11_SetWindowAlwaysOnTop(SDL_VideoDevice *_this, SDL_Window *window, bool on_top)
  1190. {
  1191. SDL_WindowData *data = window->internal;
  1192. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  1193. Display *display = data->videodata->display;
  1194. Atom _NET_WM_STATE = data->videodata->atoms._NET_WM_STATE;
  1195. Atom _NET_WM_STATE_ABOVE = data->videodata->atoms._NET_WM_STATE_ABOVE;
  1196. if (X11_IsWindowMapped(_this, window)) {
  1197. XEvent e;
  1198. SDL_zero(e);
  1199. e.xany.type = ClientMessage;
  1200. e.xclient.message_type = _NET_WM_STATE;
  1201. e.xclient.format = 32;
  1202. e.xclient.window = data->xwindow;
  1203. e.xclient.data.l[0] =
  1204. on_top ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  1205. e.xclient.data.l[1] = _NET_WM_STATE_ABOVE;
  1206. e.xclient.data.l[3] = 0l;
  1207. X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
  1208. SubstructureNotifyMask | SubstructureRedirectMask, &e);
  1209. } else {
  1210. X11_SetNetWMState(_this, data->xwindow, window->flags);
  1211. }
  1212. X11_XFlush(display);
  1213. }
  1214. void X11_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1215. {
  1216. SDL_WindowData *data = window->internal;
  1217. Display *display = data->videodata->display;
  1218. bool bActivate = SDL_GetHintBoolean(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, true);
  1219. XEvent event;
  1220. if (SDL_WINDOW_IS_POPUP(window)) {
  1221. // Update the position in case the parent moved while we were hidden
  1222. X11_ConstrainPopup(window, true);
  1223. X11_UpdateWindowPosition(window, false);
  1224. }
  1225. /* Whether XMapRaised focuses the window is based on the window type and it is
  1226. * wm specific. There isn't much we can do here */
  1227. (void)bActivate;
  1228. if (!X11_IsWindowMapped(_this, window)) {
  1229. X11_XMapRaised(display, data->xwindow);
  1230. /* Blocking wait for "MapNotify" event.
  1231. * We use X11_XIfEvent because pXWindowEvent takes a mask rather than a type,
  1232. * and XCheckTypedWindowEvent doesn't block */
  1233. if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
  1234. X11_XIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
  1235. }
  1236. X11_XFlush(display);
  1237. }
  1238. if (!data->videodata->net_wm) {
  1239. // no WM means no FocusIn event, which confuses us. Force it.
  1240. X11_XSync(display, False);
  1241. X11_XSetInputFocus(display, data->xwindow, RevertToNone, CurrentTime);
  1242. X11_XFlush(display);
  1243. }
  1244. // Popup menus grab the keyboard
  1245. if (window->flags & SDL_WINDOW_POPUP_MENU) {
  1246. X11_SetKeyboardFocus(window, window->parent == SDL_GetKeyboardFocus());
  1247. }
  1248. // Get some valid border values, if we haven't received them yet
  1249. if (data->border_left == 0 && data->border_right == 0 && data->border_top == 0 && data->border_bottom == 0) {
  1250. X11_GetBorderValues(data);
  1251. }
  1252. // Apply the pending position, if any, after the window is mapped.
  1253. data->pending_position = window->last_position_pending;
  1254. /* Some window managers can send garbage coordinates while mapping the window, so don't emit size and position
  1255. * events during the initial configure events.
  1256. */
  1257. data->size_move_event_flags = X11_SIZE_MOVE_EVENTS_DISABLE;
  1258. X11_XSync(display, False);
  1259. X11_PumpEvents(_this);
  1260. data->size_move_event_flags = 0;
  1261. // If a configure event was received (type is non-zero), send the final window size and coordinates.
  1262. if (data->last_xconfigure.type) {
  1263. int x, y;
  1264. SDL_GlobalToRelativeForWindow(data->window, data->last_xconfigure.x, data->last_xconfigure.y, &x, &y);
  1265. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, data->last_xconfigure.width, data->last_xconfigure.height);
  1266. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, x, y);
  1267. }
  1268. }
  1269. void X11_HideWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1270. {
  1271. SDL_WindowData *data = window->internal;
  1272. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  1273. int screen = (displaydata ? displaydata->screen : 0);
  1274. Display *display = data->videodata->display;
  1275. XEvent event;
  1276. if (X11_IsWindowMapped(_this, window)) {
  1277. X11_XWithdrawWindow(display, data->xwindow, screen);
  1278. // Blocking wait for "UnmapNotify" event
  1279. if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
  1280. X11_XIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
  1281. }
  1282. X11_XFlush(display);
  1283. }
  1284. // Transfer keyboard focus back to the parent
  1285. if (window->flags & SDL_WINDOW_POPUP_MENU) {
  1286. SDL_Window *new_focus = window->parent;
  1287. bool set_focus = window == SDL_GetKeyboardFocus();
  1288. // Find the highest level window, up to the toplevel parent, that isn't being hidden or destroyed.
  1289. while (SDL_WINDOW_IS_POPUP(new_focus) && (new_focus->is_hiding || new_focus->is_destroying)) {
  1290. new_focus = new_focus->parent;
  1291. // If some window in the chain currently had focus, set it to the new lowest-level window.
  1292. if (!set_focus) {
  1293. set_focus = new_focus == SDL_GetKeyboardFocus();
  1294. }
  1295. }
  1296. X11_SetKeyboardFocus(new_focus, set_focus);
  1297. }
  1298. X11_XSync(display, False);
  1299. X11_PumpEvents(_this);
  1300. }
  1301. static bool X11_SetWindowActive(SDL_VideoDevice *_this, SDL_Window *window)
  1302. {
  1303. CHECK_WINDOW_DATA(window);
  1304. SDL_WindowData *data = window->internal;
  1305. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  1306. Display *display = data->videodata->display;
  1307. Atom _NET_ACTIVE_WINDOW = data->videodata->atoms._NET_ACTIVE_WINDOW;
  1308. if (X11_IsWindowMapped(_this, window)) {
  1309. XEvent e;
  1310. // printf("SDL Window %p: sending _NET_ACTIVE_WINDOW with timestamp %lu\n", window, data->user_time);
  1311. SDL_zero(e);
  1312. e.xany.type = ClientMessage;
  1313. e.xclient.message_type = _NET_ACTIVE_WINDOW;
  1314. e.xclient.format = 32;
  1315. e.xclient.window = data->xwindow;
  1316. e.xclient.data.l[0] = 1; // source indication. 1 = application
  1317. e.xclient.data.l[1] = data->user_time;
  1318. e.xclient.data.l[2] = 0;
  1319. X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
  1320. SubstructureNotifyMask | SubstructureRedirectMask, &e);
  1321. X11_XFlush(display);
  1322. }
  1323. return true;
  1324. }
  1325. void X11_RaiseWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1326. {
  1327. SDL_WindowData *data = window->internal;
  1328. Display *display = data->videodata->display;
  1329. bool bActivate = SDL_GetHintBoolean(SDL_HINT_WINDOW_ACTIVATE_WHEN_RAISED, true);
  1330. X11_XRaiseWindow(display, data->xwindow);
  1331. if (bActivate) {
  1332. X11_SetWindowActive(_this, window);
  1333. }
  1334. X11_XFlush(display);
  1335. }
  1336. static bool X11_SetWindowMaximized(SDL_VideoDevice *_this, SDL_Window *window, bool maximized)
  1337. {
  1338. CHECK_WINDOW_DATA(window);
  1339. SDL_WindowData *data = window->internal;
  1340. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  1341. Display *display = data->videodata->display;
  1342. Atom _NET_WM_STATE = data->videodata->atoms._NET_WM_STATE;
  1343. Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->atoms._NET_WM_STATE_MAXIMIZED_VERT;
  1344. Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->atoms._NET_WM_STATE_MAXIMIZED_HORZ;
  1345. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1346. /* Fullscreen windows are maximized on some window managers,
  1347. and this is functional behavior, so don't remove that state
  1348. now, we'll take care of it when we leave fullscreen mode.
  1349. */
  1350. return true;
  1351. }
  1352. if (X11_IsWindowMapped(_this, window)) {
  1353. XEvent e;
  1354. SDL_zero(e);
  1355. e.xany.type = ClientMessage;
  1356. e.xclient.message_type = _NET_WM_STATE;
  1357. e.xclient.format = 32;
  1358. e.xclient.window = data->xwindow;
  1359. e.xclient.data.l[0] =
  1360. maximized ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  1361. e.xclient.data.l[1] = _NET_WM_STATE_MAXIMIZED_VERT;
  1362. e.xclient.data.l[2] = _NET_WM_STATE_MAXIMIZED_HORZ;
  1363. e.xclient.data.l[3] = 0l;
  1364. if (maximized) {
  1365. SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
  1366. SDL_Rect bounds;
  1367. SDL_zero(bounds);
  1368. SDL_GetDisplayUsableBounds(displayID, &bounds);
  1369. data->expected.x = bounds.x + data->border_left;
  1370. data->expected.y = bounds.y + data->border_top;
  1371. data->expected.w = bounds.w - (data->border_left + data->border_right);
  1372. data->expected.h = bounds.h - (data->border_top + data->border_bottom);
  1373. } else {
  1374. data->expected.x = window->floating.x;
  1375. data->expected.y = window->floating.y;
  1376. data->expected.w = window->floating.w;
  1377. data->expected.h = window->floating.h;
  1378. }
  1379. X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
  1380. SubstructureNotifyMask | SubstructureRedirectMask, &e);
  1381. } else {
  1382. X11_SetNetWMState(_this, data->xwindow, window->flags);
  1383. }
  1384. X11_XFlush(display);
  1385. return true;
  1386. }
  1387. void X11_MaximizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1388. {
  1389. if (window->internal->pending_operation & (X11_PENDING_OP_FULLSCREEN | X11_PENDING_OP_MINIMIZE)) {
  1390. SDL_SyncWindow(window);
  1391. }
  1392. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1393. // If fullscreen, just toggle the restored state.
  1394. window->internal->window_was_maximized = true;
  1395. return;
  1396. }
  1397. if (!(window->flags & SDL_WINDOW_MINIMIZED)) {
  1398. window->internal->pending_operation |= X11_PENDING_OP_MAXIMIZE;
  1399. X11_SetWindowMaximized(_this, window, true);
  1400. }
  1401. }
  1402. void X11_MinimizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1403. {
  1404. SDL_WindowData *data = window->internal;
  1405. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  1406. Display *display = data->videodata->display;
  1407. if (data->pending_operation & SDL_WINDOW_FULLSCREEN) {
  1408. SDL_SyncWindow(window);
  1409. }
  1410. data->pending_operation |= X11_PENDING_OP_MINIMIZE;
  1411. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1412. data->window_was_maximized = !!(window->flags & SDL_WINDOW_MAXIMIZED);
  1413. }
  1414. X11_XIconifyWindow(display, data->xwindow, displaydata->screen);
  1415. X11_XFlush(display);
  1416. }
  1417. void X11_RestoreWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1418. {
  1419. if (window->internal->pending_operation & (X11_PENDING_OP_FULLSCREEN | X11_PENDING_OP_MAXIMIZE | X11_PENDING_OP_MINIMIZE)) {
  1420. SDL_SyncWindow(window);
  1421. }
  1422. if ((window->flags & SDL_WINDOW_FULLSCREEN) && !(window->flags & SDL_WINDOW_MINIMIZED)) {
  1423. // If fullscreen and not minimized, just toggle the restored state.
  1424. window->internal->window_was_maximized = false;
  1425. return;
  1426. }
  1427. if (window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED) ||
  1428. (window->internal->pending_operation & X11_PENDING_OP_MINIMIZE)) {
  1429. window->internal->pending_operation |= X11_PENDING_OP_RESTORE;
  1430. }
  1431. // If the window was minimized while maximized, restore as maximized.
  1432. const bool maximize = !!(window->flags & SDL_WINDOW_MINIMIZED) && window->internal->window_was_maximized;
  1433. X11_SetWindowMaximized(_this, window, maximize);
  1434. X11_ShowWindow(_this, window);
  1435. X11_SetWindowActive(_this, window);
  1436. }
  1437. // This asks the Window Manager to handle fullscreen for us. This is the modern way.
  1438. static SDL_FullscreenResult X11_SetWindowFullscreenViaWM(SDL_VideoDevice *_this, SDL_Window *window, SDL_VideoDisplay *_display, SDL_FullscreenOp fullscreen)
  1439. {
  1440. CHECK_WINDOW_DATA(window);
  1441. CHECK_DISPLAY_DATA(_display);
  1442. SDL_WindowData *data = window->internal;
  1443. SDL_DisplayData *displaydata = _display->internal;
  1444. Display *display = data->videodata->display;
  1445. Atom _NET_WM_STATE = data->videodata->atoms._NET_WM_STATE;
  1446. Atom _NET_WM_STATE_FULLSCREEN = data->videodata->atoms._NET_WM_STATE_FULLSCREEN;
  1447. if (X11_IsWindowMapped(_this, window)) {
  1448. XEvent e;
  1449. // Flush any pending fullscreen events.
  1450. if (data->pending_operation & (X11_PENDING_OP_FULLSCREEN | X11_PENDING_OP_MAXIMIZE | X11_PENDING_OP_MOVE)) {
  1451. X11_SyncWindow(_this, window);
  1452. }
  1453. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1454. if (fullscreen == SDL_FULLSCREEN_OP_UPDATE) {
  1455. // Request was out of date; set -1 to signal the video core to undo a mode switch.
  1456. return SDL_FULLSCREEN_FAILED;
  1457. } else if (fullscreen == SDL_FULLSCREEN_OP_LEAVE) {
  1458. // Nothing to do.
  1459. return SDL_FULLSCREEN_SUCCEEDED;
  1460. }
  1461. }
  1462. if (fullscreen && !(window->flags & SDL_WINDOW_RESIZABLE)) {
  1463. /* Compiz refuses fullscreen toggle if we're not resizable, so update the hints so we
  1464. can be resized to the fullscreen resolution (or reset so we're not resizable again) */
  1465. XSizeHints *sizehints = X11_XAllocSizeHints();
  1466. long flags = 0;
  1467. X11_XGetWMNormalHints(display, data->xwindow, sizehints, &flags);
  1468. // we are going fullscreen so turn the flags off
  1469. sizehints->flags &= ~(PMinSize | PMaxSize | PAspect);
  1470. X11_XSetWMNormalHints(display, data->xwindow, sizehints);
  1471. X11_XFree(sizehints);
  1472. }
  1473. SDL_zero(e);
  1474. e.xany.type = ClientMessage;
  1475. e.xclient.message_type = _NET_WM_STATE;
  1476. e.xclient.format = 32;
  1477. e.xclient.window = data->xwindow;
  1478. e.xclient.data.l[0] =
  1479. fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  1480. e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
  1481. e.xclient.data.l[3] = 0l;
  1482. X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
  1483. SubstructureNotifyMask | SubstructureRedirectMask, &e);
  1484. if (!!(window->flags & SDL_WINDOW_FULLSCREEN) != fullscreen) {
  1485. data->pending_operation |= X11_PENDING_OP_FULLSCREEN;
  1486. }
  1487. // Set the position so the window will be on the target display
  1488. if (fullscreen) {
  1489. SDL_DisplayID current = SDL_GetDisplayForWindowPosition(window);
  1490. SDL_copyp(&data->requested_fullscreen_mode, &window->current_fullscreen_mode);
  1491. if (fullscreen != !!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1492. data->window_was_maximized = !!(window->flags & SDL_WINDOW_MAXIMIZED);
  1493. }
  1494. data->expected.x = displaydata->x;
  1495. data->expected.y = displaydata->y;
  1496. data->expected.w = _display->current_mode->w;
  1497. data->expected.h = _display->current_mode->h;
  1498. // Only move the window if it isn't fullscreen or already on the target display.
  1499. if (!(window->flags & SDL_WINDOW_FULLSCREEN) || (!current || current != _display->id)) {
  1500. X11_XMoveWindow(display, data->xwindow, displaydata->x, displaydata->y);
  1501. data->pending_operation |= X11_PENDING_OP_MOVE;
  1502. }
  1503. } else {
  1504. SDL_zero(data->requested_fullscreen_mode);
  1505. /* Fullscreen windows sometimes end up being marked maximized by
  1506. * window managers. Force it back to how we expect it to be.
  1507. */
  1508. SDL_zero(e);
  1509. e.xany.type = ClientMessage;
  1510. e.xclient.message_type = _NET_WM_STATE;
  1511. e.xclient.format = 32;
  1512. e.xclient.window = data->xwindow;
  1513. if (data->window_was_maximized) {
  1514. e.xclient.data.l[0] = _NET_WM_STATE_ADD;
  1515. } else {
  1516. e.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
  1517. }
  1518. e.xclient.data.l[1] = data->videodata->atoms._NET_WM_STATE_MAXIMIZED_VERT;
  1519. e.xclient.data.l[2] = data->videodata->atoms._NET_WM_STATE_MAXIMIZED_HORZ;
  1520. e.xclient.data.l[3] = 0l;
  1521. X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
  1522. SubstructureNotifyMask | SubstructureRedirectMask, &e);
  1523. }
  1524. } else {
  1525. SDL_WindowFlags flags;
  1526. flags = window->flags;
  1527. if (fullscreen) {
  1528. flags |= SDL_WINDOW_FULLSCREEN;
  1529. } else {
  1530. flags &= ~SDL_WINDOW_FULLSCREEN;
  1531. }
  1532. X11_SetNetWMState(_this, data->xwindow, flags);
  1533. }
  1534. if (data->visual->class == DirectColor) {
  1535. if (fullscreen) {
  1536. X11_XInstallColormap(display, data->colormap);
  1537. } else {
  1538. X11_XUninstallColormap(display, data->colormap);
  1539. }
  1540. }
  1541. return SDL_FULLSCREEN_PENDING;
  1542. }
  1543. SDL_FullscreenResult X11_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window *window, SDL_VideoDisplay *_display, SDL_FullscreenOp fullscreen)
  1544. {
  1545. return X11_SetWindowFullscreenViaWM(_this, window, _display, fullscreen);
  1546. }
  1547. typedef struct
  1548. {
  1549. unsigned char *data;
  1550. int format, count;
  1551. Atom type;
  1552. } SDL_x11Prop;
  1553. /* Reads property
  1554. Must call X11_XFree on results
  1555. */
  1556. static void X11_ReadProperty(SDL_x11Prop *p, Display *disp, Window w, Atom prop)
  1557. {
  1558. unsigned char *ret = NULL;
  1559. Atom type;
  1560. int fmt;
  1561. unsigned long count;
  1562. unsigned long bytes_left;
  1563. int bytes_fetch = 0;
  1564. do {
  1565. if (ret) {
  1566. X11_XFree(ret);
  1567. }
  1568. X11_XGetWindowProperty(disp, w, prop, 0, bytes_fetch, False, AnyPropertyType, &type, &fmt, &count, &bytes_left, &ret);
  1569. bytes_fetch += bytes_left;
  1570. } while (bytes_left != 0);
  1571. p->data = ret;
  1572. p->format = fmt;
  1573. p->count = count;
  1574. p->type = type;
  1575. }
  1576. void *X11_GetWindowICCProfile(SDL_VideoDevice *_this, SDL_Window *window, size_t *size)
  1577. {
  1578. SDL_WindowData *data = window->internal;
  1579. Display *display = data->videodata->display;
  1580. XWindowAttributes attributes;
  1581. Atom icc_profile_atom;
  1582. char icc_atom_string[sizeof("_ICC_PROFILE_") + 12];
  1583. void *ret_icc_profile_data = NULL;
  1584. CARD8 *icc_profile_data;
  1585. int real_format;
  1586. unsigned long real_nitems;
  1587. SDL_x11Prop atomProp;
  1588. X11_XGetWindowAttributes(display, data->xwindow, &attributes);
  1589. if (X11_XScreenNumberOfScreen(attributes.screen) > 0) {
  1590. (void)SDL_snprintf(icc_atom_string, sizeof("_ICC_PROFILE_") + 12, "%s%d", "_ICC_PROFILE_", X11_XScreenNumberOfScreen(attributes.screen));
  1591. } else {
  1592. SDL_strlcpy(icc_atom_string, "_ICC_PROFILE", sizeof("_ICC_PROFILE"));
  1593. }
  1594. X11_XGetWindowAttributes(display, RootWindowOfScreen(attributes.screen), &attributes);
  1595. icc_profile_atom = X11_XInternAtom(display, icc_atom_string, True);
  1596. if (icc_profile_atom == None) {
  1597. SDL_SetError("Screen is not calibrated.");
  1598. return NULL;
  1599. }
  1600. X11_ReadProperty(&atomProp, display, RootWindowOfScreen(attributes.screen), icc_profile_atom);
  1601. real_format = atomProp.format;
  1602. real_nitems = atomProp.count;
  1603. icc_profile_data = atomProp.data;
  1604. if (real_format == None) {
  1605. SDL_SetError("Screen is not calibrated.");
  1606. return NULL;
  1607. }
  1608. ret_icc_profile_data = SDL_malloc(real_nitems);
  1609. if (!ret_icc_profile_data) {
  1610. return NULL;
  1611. }
  1612. SDL_memcpy(ret_icc_profile_data, icc_profile_data, real_nitems);
  1613. *size = real_nitems;
  1614. X11_XFree(icc_profile_data);
  1615. return ret_icc_profile_data;
  1616. }
  1617. bool X11_SetWindowMouseGrab(SDL_VideoDevice *_this, SDL_Window *window, bool grabbed)
  1618. {
  1619. SDL_WindowData *data = window->internal;
  1620. Display *display;
  1621. if (!data) {
  1622. return SDL_SetError("Invalid window data");
  1623. }
  1624. data->mouse_grabbed = false;
  1625. display = data->videodata->display;
  1626. if (grabbed) {
  1627. /* If the window is unmapped, XGrab calls return GrabNotViewable,
  1628. so when we get a MapNotify later, we'll try to update the grab as
  1629. appropriate. */
  1630. if (window->flags & SDL_WINDOW_HIDDEN) {
  1631. return true;
  1632. }
  1633. /* If XInput2 is enabled, it will grab the pointer on button presses,
  1634. * which results in XGrabPointer returning AlreadyGrabbed. If buttons
  1635. * are currently pressed, clear any existing grabs before attempting
  1636. * the confinement grab.
  1637. */
  1638. if (data->xinput2_mouse_enabled && SDL_GetMouseState(NULL, NULL)) {
  1639. X11_XUngrabPointer(display, CurrentTime);
  1640. }
  1641. // Try to grab the mouse
  1642. if (!data->videodata->broken_pointer_grab) {
  1643. const unsigned int mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask;
  1644. int attempts;
  1645. int result = 0;
  1646. // Try for up to 5000ms (5s) to grab. If it still fails, stop trying.
  1647. for (attempts = 0; attempts < 100; attempts++) {
  1648. result = X11_XGrabPointer(display, data->xwindow, False, mask, GrabModeAsync,
  1649. GrabModeAsync, data->xwindow, None, CurrentTime);
  1650. if (result == GrabSuccess) {
  1651. data->mouse_grabbed = true;
  1652. break;
  1653. }
  1654. SDL_Delay(50);
  1655. }
  1656. if (result != GrabSuccess) {
  1657. data->videodata->broken_pointer_grab = true; // don't try again.
  1658. }
  1659. }
  1660. X11_Xinput2GrabTouch(_this, window);
  1661. // Raise the window if we grab the mouse
  1662. X11_XRaiseWindow(display, data->xwindow);
  1663. } else {
  1664. X11_XUngrabPointer(display, CurrentTime);
  1665. X11_Xinput2UngrabTouch(_this, window);
  1666. }
  1667. X11_XSync(display, False);
  1668. if (!data->videodata->broken_pointer_grab) {
  1669. return true;
  1670. } else {
  1671. return SDL_SetError("The X server refused to let us grab the mouse. You might experience input bugs.");
  1672. }
  1673. }
  1674. bool X11_SetWindowKeyboardGrab(SDL_VideoDevice *_this, SDL_Window *window, bool grabbed)
  1675. {
  1676. SDL_WindowData *data = window->internal;
  1677. Display *display;
  1678. if (!data) {
  1679. return SDL_SetError("Invalid window data");
  1680. }
  1681. display = data->videodata->display;
  1682. if (grabbed) {
  1683. /* If the window is unmapped, XGrab calls return GrabNotViewable,
  1684. so when we get a MapNotify later, we'll try to update the grab as
  1685. appropriate. */
  1686. if (window->flags & SDL_WINDOW_HIDDEN) {
  1687. return true;
  1688. }
  1689. X11_XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
  1690. GrabModeAsync, CurrentTime);
  1691. } else {
  1692. X11_XUngrabKeyboard(display, CurrentTime);
  1693. }
  1694. X11_XSync(display, False);
  1695. return true;
  1696. }
  1697. void X11_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1698. {
  1699. SDL_WindowData *data = window->internal;
  1700. if (data) {
  1701. SDL_VideoData *videodata = data->videodata;
  1702. Display *display = videodata->display;
  1703. int numwindows = videodata->numwindows;
  1704. SDL_WindowData **windowlist = videodata->windowlist;
  1705. int i;
  1706. if (windowlist) {
  1707. for (i = 0; i < numwindows; ++i) {
  1708. if (windowlist[i] && (windowlist[i]->window == window)) {
  1709. windowlist[i] = windowlist[numwindows - 1];
  1710. windowlist[numwindows - 1] = NULL;
  1711. videodata->numwindows--;
  1712. break;
  1713. }
  1714. }
  1715. }
  1716. #ifdef X_HAVE_UTF8_STRING
  1717. if (data->ic) {
  1718. X11_XDestroyIC(data->ic);
  1719. SDL_free(data->preedit_text);
  1720. SDL_free(data->preedit_feedback);
  1721. }
  1722. #endif
  1723. #ifdef SDL_VIDEO_DRIVER_X11_XSYNC
  1724. X11_TermResizeSync(window);
  1725. #endif /* SDL_VIDEO_DRIVER_X11_XSYNC */
  1726. if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
  1727. X11_XDestroyWindow(display, data->xwindow);
  1728. X11_XFlush(display);
  1729. }
  1730. SDL_free(data);
  1731. #ifdef SDL_VIDEO_DRIVER_X11_XFIXES
  1732. // If the pointer barriers are active for this, deactivate it.
  1733. if (videodata->active_cursor_confined_window == window) {
  1734. X11_DestroyPointerBarrier(_this, window);
  1735. }
  1736. #endif // SDL_VIDEO_DRIVER_X11_XFIXES
  1737. }
  1738. window->internal = NULL;
  1739. }
  1740. bool X11_SetWindowHitTest(SDL_Window *window, bool enabled)
  1741. {
  1742. return true; // just succeed, the real work is done elsewhere.
  1743. }
  1744. void X11_AcceptDragAndDrop(SDL_Window *window, bool accept)
  1745. {
  1746. SDL_WindowData *data = window->internal;
  1747. Display *display = data->videodata->display;
  1748. Atom XdndAware = data->videodata->atoms.XdndAware;
  1749. if (accept) {
  1750. Atom xdnd_version = 5;
  1751. X11_XChangeProperty(display, data->xwindow, XdndAware, XA_ATOM, 32,
  1752. PropModeReplace, (unsigned char *)&xdnd_version, 1);
  1753. } else {
  1754. X11_XDeleteProperty(display, data->xwindow, XdndAware);
  1755. }
  1756. }
  1757. bool X11_FlashWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_FlashOperation operation)
  1758. {
  1759. SDL_WindowData *data = window->internal;
  1760. Display *display = data->videodata->display;
  1761. XWMHints *wmhints;
  1762. wmhints = X11_XGetWMHints(display, data->xwindow);
  1763. if (!wmhints) {
  1764. return SDL_SetError("Couldn't get WM hints");
  1765. }
  1766. wmhints->flags &= ~XUrgencyHint;
  1767. data->flashing_window = false;
  1768. data->flash_cancel_time = 0;
  1769. switch (operation) {
  1770. case SDL_FLASH_CANCEL:
  1771. // Taken care of above
  1772. break;
  1773. case SDL_FLASH_BRIEFLY:
  1774. if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  1775. wmhints->flags |= XUrgencyHint;
  1776. data->flashing_window = true;
  1777. // On Ubuntu 21.04 this causes a dialog to pop up, so leave it up for a full second so users can see it
  1778. data->flash_cancel_time = SDL_GetTicks() + 1000;
  1779. }
  1780. break;
  1781. case SDL_FLASH_UNTIL_FOCUSED:
  1782. if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  1783. wmhints->flags |= XUrgencyHint;
  1784. data->flashing_window = true;
  1785. }
  1786. break;
  1787. default:
  1788. break;
  1789. }
  1790. X11_XSetWMHints(display, data->xwindow, wmhints);
  1791. X11_XFree(wmhints);
  1792. return true;
  1793. }
  1794. bool SDL_X11_SetWindowTitle(Display *display, Window xwindow, char *title)
  1795. {
  1796. Atom _NET_WM_NAME = X11_XInternAtom(display, "_NET_WM_NAME", False);
  1797. XTextProperty titleprop;
  1798. int conv = X11_XmbTextListToTextProperty(display, &title, 1, XTextStyle, &titleprop);
  1799. Status status;
  1800. if (X11_XSupportsLocale() != True) {
  1801. return SDL_SetError("Current locale not supported by X server, cannot continue.");
  1802. }
  1803. if (conv == 0) {
  1804. X11_XSetTextProperty(display, xwindow, &titleprop, XA_WM_NAME);
  1805. X11_XFree(titleprop.value);
  1806. // we know this can't be a locale error as we checked X locale validity
  1807. } else if (conv < 0) {
  1808. return SDL_OutOfMemory();
  1809. } else { // conv > 0
  1810. SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "%d characters were not convertible to the current locale!", conv);
  1811. return true;
  1812. }
  1813. #ifdef X_HAVE_UTF8_STRING
  1814. status = X11_Xutf8TextListToTextProperty(display, &title, 1, XUTF8StringStyle, &titleprop);
  1815. if (status == Success) {
  1816. X11_XSetTextProperty(display, xwindow, &titleprop, _NET_WM_NAME);
  1817. X11_XFree(titleprop.value);
  1818. } else {
  1819. return SDL_SetError("Failed to convert title to UTF8! Bad encoding, or bad Xorg encoding? Window title: «%s»", title);
  1820. }
  1821. #endif
  1822. X11_XFlush(display);
  1823. return true;
  1824. }
  1825. void X11_ShowWindowSystemMenu(SDL_Window *window, int x, int y)
  1826. {
  1827. SDL_WindowData *data = window->internal;
  1828. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  1829. Display *display = data->videodata->display;
  1830. Window root = RootWindow(display, displaydata->screen);
  1831. XClientMessageEvent e;
  1832. Window childReturn;
  1833. int wx, wy;
  1834. SDL_zero(e);
  1835. X11_XTranslateCoordinates(display, data->xwindow, root, x, y, &wx, &wy, &childReturn);
  1836. e.type = ClientMessage;
  1837. e.window = data->xwindow;
  1838. e.message_type = X11_XInternAtom(display, "_GTK_SHOW_WINDOW_MENU", 0);
  1839. e.data.l[0] = 0; // GTK device ID (unused)
  1840. e.data.l[1] = wx; // X coordinate relative to root
  1841. e.data.l[2] = wy; // Y coordinate relative to root
  1842. e.format = 32;
  1843. X11_XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&e);
  1844. X11_XFlush(display);
  1845. }
  1846. bool X11_SyncWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1847. {
  1848. const Uint64 current_time = SDL_GetTicksNS();
  1849. Uint64 timeout = 0;
  1850. // Allow time for any pending mode switches to complete.
  1851. for (int i = 0; i < _this->num_displays; ++i) {
  1852. if (_this->displays[i]->internal->mode_switch_deadline_ns &&
  1853. current_time < _this->displays[i]->internal->mode_switch_deadline_ns) {
  1854. timeout = SDL_max(_this->displays[i]->internal->mode_switch_deadline_ns - current_time, timeout);
  1855. }
  1856. }
  1857. /* 100ms is fine for most cases, but, for some reason, maximizing
  1858. * a window can take a very long time.
  1859. */
  1860. timeout += window->internal->pending_operation & X11_PENDING_OP_MAXIMIZE ? SDL_MS_TO_NS(1000) : SDL_MS_TO_NS(100);
  1861. return X11_SyncWindowTimeout(_this, window, timeout);
  1862. }
  1863. bool X11_SetWindowFocusable(SDL_VideoDevice *_this, SDL_Window *window, bool focusable)
  1864. {
  1865. SDL_WindowData *data = window->internal;
  1866. Display *display = data->videodata->display;
  1867. XWMHints *wmhints;
  1868. wmhints = X11_XGetWMHints(display, data->xwindow);
  1869. if (!wmhints) {
  1870. return SDL_SetError("Couldn't get WM hints");
  1871. }
  1872. wmhints->input = focusable ? True : False;
  1873. wmhints->flags |= InputHint;
  1874. X11_XSetWMHints(display, data->xwindow, wmhints);
  1875. X11_XFree(wmhints);
  1876. return true;
  1877. }
  1878. #endif // SDL_VIDEO_DRIVER_X11