SDL_waylandwindow.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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_WAYLAND
  20. #include "../SDL_sysvideo.h"
  21. #include "../../events/SDL_events_c.h"
  22. #include "../../core/unix/SDL_appid.h"
  23. #include "../SDL_egl_c.h"
  24. #include "SDL_waylandevents_c.h"
  25. #include "SDL_waylandwindow.h"
  26. #include "SDL_waylandvideo.h"
  27. #include "../../SDL_hints_c.h"
  28. #include "alpha-modifier-v1-client-protocol.h"
  29. #include "xdg-shell-client-protocol.h"
  30. #include "xdg-decoration-unstable-v1-client-protocol.h"
  31. #include "idle-inhibit-unstable-v1-client-protocol.h"
  32. #include "xdg-activation-v1-client-protocol.h"
  33. #include "viewporter-client-protocol.h"
  34. #include "fractional-scale-v1-client-protocol.h"
  35. #include "xdg-foreign-unstable-v2-client-protocol.h"
  36. #include "xdg-dialog-v1-client-protocol.h"
  37. #include "frog-color-management-v1-client-protocol.h"
  38. #include "xdg-toplevel-icon-v1-client-protocol.h"
  39. #ifdef HAVE_LIBDECOR_H
  40. #include <libdecor.h>
  41. #endif
  42. static double GetWindowScale(SDL_Window *window)
  43. {
  44. return (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) || window->internal->scale_to_display ? window->internal->scale_factor : 1.0;
  45. }
  46. // These are point->pixel->point round trip safe; the inverse is not round trip safe due to rounding.
  47. static int PointToPixel(SDL_Window *window, int point)
  48. {
  49. /* Rounds halfway away from zero as per the Wayland fractional scaling protocol spec.
  50. * Wayland scale units are in units of 1/120, so the offset is required to correct for
  51. * rounding errors when using certain scale values.
  52. */
  53. return SDL_max((int)SDL_lround((double)point * GetWindowScale(window) + 1e-6), 1);
  54. }
  55. static int PixelToPoint(SDL_Window *window, int pixel)
  56. {
  57. return SDL_max((int)SDL_lround((double)pixel / GetWindowScale(window)), 1);
  58. }
  59. /* According to the Wayland spec:
  60. *
  61. * "If the [fullscreen] surface doesn't cover the whole output, the compositor will
  62. * position the surface in the center of the output and compensate with border fill
  63. * covering the rest of the output. The content of the border fill is undefined, but
  64. * should be assumed to be in some way that attempts to blend into the surrounding area
  65. * (e.g. solid black)."
  66. *
  67. * - KDE, as of 5.27, still doesn't do this
  68. * - GNOME prior to 43 didn't do this (older versions are still found in many LTS distros)
  69. *
  70. * Default to 'stretch' for now, until things have moved forward enough that the default
  71. * can be changed to 'aspect'.
  72. */
  73. enum WaylandModeScale
  74. {
  75. WAYLAND_MODE_SCALE_UNDEFINED,
  76. WAYLAND_MODE_SCALE_ASPECT,
  77. WAYLAND_MODE_SCALE_STRETCH,
  78. WAYLAND_MODE_SCALE_NONE
  79. };
  80. static enum WaylandModeScale GetModeScaleMethod(void)
  81. {
  82. static enum WaylandModeScale scale_mode = WAYLAND_MODE_SCALE_UNDEFINED;
  83. if (scale_mode == WAYLAND_MODE_SCALE_UNDEFINED) {
  84. const char *scale_hint = SDL_GetHint(SDL_HINT_VIDEO_WAYLAND_MODE_SCALING);
  85. if (scale_hint) {
  86. if (!SDL_strcasecmp(scale_hint, "aspect")) {
  87. scale_mode = WAYLAND_MODE_SCALE_ASPECT;
  88. } else if (!SDL_strcasecmp(scale_hint, "none")) {
  89. scale_mode = WAYLAND_MODE_SCALE_NONE;
  90. } else {
  91. scale_mode = WAYLAND_MODE_SCALE_STRETCH;
  92. }
  93. } else {
  94. scale_mode = WAYLAND_MODE_SCALE_STRETCH;
  95. }
  96. }
  97. return scale_mode;
  98. }
  99. static void GetBufferSize(SDL_Window *window, int *width, int *height)
  100. {
  101. SDL_WindowData *data = window->internal;
  102. int buf_width;
  103. int buf_height;
  104. // Exclusive fullscreen modes always have a pixel density of 1
  105. if (data->is_fullscreen && window->fullscreen_exclusive) {
  106. buf_width = window->current_fullscreen_mode.w;
  107. buf_height = window->current_fullscreen_mode.h;
  108. } else if (!data->scale_to_display) {
  109. // Round fractional backbuffer sizes halfway away from zero.
  110. buf_width = PointToPixel(window, data->requested.logical_width);
  111. buf_height = PointToPixel(window, data->requested.logical_height);
  112. } else {
  113. buf_width = data->requested.pixel_width;
  114. buf_height = data->requested.pixel_height;
  115. }
  116. if (width) {
  117. *width = buf_width;
  118. }
  119. if (height) {
  120. *height = buf_height;
  121. }
  122. }
  123. static void SetMinMaxDimensions(SDL_Window *window)
  124. {
  125. SDL_WindowData *wind = window->internal;
  126. int min_width, min_height, max_width, max_height;
  127. if ((window->flags & SDL_WINDOW_FULLSCREEN) || wind->fullscreen_deadline_count) {
  128. min_width = 0;
  129. min_height = 0;
  130. max_width = 0;
  131. max_height = 0;
  132. } else if (window->flags & SDL_WINDOW_RESIZABLE) {
  133. int adj_w = SDL_max(window->min_w, wind->system_limits.min_width);
  134. int adj_h = SDL_max(window->min_h, wind->system_limits.min_height);
  135. if (wind->scale_to_display) {
  136. adj_w = PixelToPoint(window, adj_w);
  137. adj_h = PixelToPoint(window, adj_h);
  138. }
  139. min_width = adj_w;
  140. min_height = adj_h;
  141. adj_w = window->max_w ? SDL_max(window->max_w, wind->system_limits.min_width) : 0;
  142. adj_h = window->max_h ? SDL_max(window->max_h, wind->system_limits.min_height) : 0;
  143. if (wind->scale_to_display) {
  144. adj_w = PixelToPoint(window, adj_w);
  145. adj_h = PixelToPoint(window, adj_h);
  146. }
  147. max_width = adj_w;
  148. max_height = adj_h;
  149. } else {
  150. min_width = wind->current.logical_width;
  151. min_height = wind->current.logical_height;
  152. max_width = wind->current.logical_width;
  153. max_height = wind->current.logical_height;
  154. }
  155. #ifdef HAVE_LIBDECOR_H
  156. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  157. if (!wind->shell_surface.libdecor.initial_configure_seen || !wind->shell_surface.libdecor.frame) {
  158. return; // Can't do anything yet, wait for ShowWindow
  159. }
  160. /* No need to change these values if the window is non-resizable,
  161. * as libdecor will just overwrite them internally.
  162. */
  163. if (libdecor_frame_has_capability(wind->shell_surface.libdecor.frame, LIBDECOR_ACTION_RESIZE)) {
  164. libdecor_frame_set_min_content_size(wind->shell_surface.libdecor.frame,
  165. min_width,
  166. min_height);
  167. libdecor_frame_set_max_content_size(wind->shell_surface.libdecor.frame,
  168. max_width,
  169. max_height);
  170. }
  171. } else
  172. #endif
  173. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
  174. if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
  175. return; // Can't do anything yet, wait for ShowWindow
  176. }
  177. xdg_toplevel_set_min_size(wind->shell_surface.xdg.roleobj.toplevel,
  178. min_width,
  179. min_height);
  180. xdg_toplevel_set_max_size(wind->shell_surface.xdg.roleobj.toplevel,
  181. max_width,
  182. max_height);
  183. }
  184. }
  185. static void EnsurePopupPositionIsValid(SDL_Window *window, int *x, int *y)
  186. {
  187. int adj_count = 0;
  188. /* Per the xdg-positioner spec, child popup windows must intersect or at
  189. * least be partially adjacent to the parent window.
  190. *
  191. * Failure to ensure this on a compositor that enforces this restriction
  192. * can result in behavior ranging from the window being spuriously closed
  193. * to a protocol violation.
  194. */
  195. if (*x + window->w < 0) {
  196. *x = -window->w;
  197. ++adj_count;
  198. }
  199. if (*y + window->h < 0) {
  200. *y = -window->h;
  201. ++adj_count;
  202. }
  203. if (*x > window->parent->w) {
  204. *x = window->parent->w;
  205. ++adj_count;
  206. }
  207. if (*y > window->parent->h) {
  208. *y = window->parent->h;
  209. ++adj_count;
  210. }
  211. /* If adjustment was required on the x and y axes, the popup is aligned with
  212. * the parent corner-to-corner and is neither overlapping nor adjacent, so it
  213. * must be nudged by 1 to be considered adjacent.
  214. */
  215. if (adj_count > 1) {
  216. *x += *x < 0 ? 1 : -1;
  217. }
  218. }
  219. static void AdjustPopupOffset(SDL_Window *popup, int *x, int *y)
  220. {
  221. // Adjust the popup positioning, if necessary
  222. #ifdef HAVE_LIBDECOR_H
  223. if (popup->parent->internal->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  224. int adj_x, adj_y;
  225. libdecor_frame_translate_coordinate(popup->parent->internal->shell_surface.libdecor.frame,
  226. *x, *y, &adj_x, &adj_y);
  227. *x = adj_x;
  228. *y = adj_y;
  229. }
  230. #endif
  231. }
  232. static void RepositionPopup(SDL_Window *window, bool use_current_position)
  233. {
  234. SDL_WindowData *wind = window->internal;
  235. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_POPUP &&
  236. wind->shell_surface.xdg.roleobj.popup.positioner &&
  237. xdg_popup_get_version(wind->shell_surface.xdg.roleobj.popup.popup) >= XDG_POPUP_REPOSITION_SINCE_VERSION) {
  238. int x = use_current_position ? window->x : window->floating.x;
  239. int y = use_current_position ? window->y : window->floating.y;
  240. EnsurePopupPositionIsValid(window, &x, &y);
  241. if (wind->scale_to_display) {
  242. x = PixelToPoint(window->parent, x);
  243. y = PixelToPoint(window->parent, y);
  244. }
  245. AdjustPopupOffset(window, &x, &y);
  246. xdg_positioner_set_anchor_rect(wind->shell_surface.xdg.roleobj.popup.positioner, 0, 0, window->parent->internal->current.logical_width, window->parent->internal->current.logical_height);
  247. xdg_positioner_set_size(wind->shell_surface.xdg.roleobj.popup.positioner, wind->current.logical_width, wind->current.logical_height);
  248. xdg_positioner_set_offset(wind->shell_surface.xdg.roleobj.popup.positioner, x, y);
  249. xdg_popup_reposition(wind->shell_surface.xdg.roleobj.popup.popup,
  250. wind->shell_surface.xdg.roleobj.popup.positioner,
  251. 0);
  252. }
  253. }
  254. static void SetSurfaceOpaqueRegion(SDL_WindowData *wind, bool is_opaque)
  255. {
  256. SDL_VideoData *viddata = wind->waylandData;
  257. if (is_opaque) {
  258. struct wl_region *region = wl_compositor_create_region(viddata->compositor);
  259. wl_region_add(region, 0, 0,
  260. wind->current.logical_width, wind->current.logical_height);
  261. wl_surface_set_opaque_region(wind->surface, region);
  262. wl_region_destroy(region);
  263. } else {
  264. wl_surface_set_opaque_region(wind->surface, NULL);
  265. }
  266. }
  267. static void ConfigureWindowGeometry(SDL_Window *window)
  268. {
  269. SDL_WindowData *data = window->internal;
  270. const double scale_factor = GetWindowScale(window);
  271. const int old_pixel_width = data->current.pixel_width;
  272. const int old_pixel_height = data->current.pixel_height;
  273. int window_width, window_height;
  274. bool window_size_changed;
  275. // Set the drawable backbuffer size.
  276. GetBufferSize(window, &data->current.pixel_width, &data->current.pixel_height);
  277. const bool buffer_size_changed = data->current.pixel_width != old_pixel_width ||
  278. data->current.pixel_height != old_pixel_height;
  279. if (data->egl_window && buffer_size_changed) {
  280. WAYLAND_wl_egl_window_resize(data->egl_window,
  281. data->current.pixel_width,
  282. data->current.pixel_height,
  283. 0, 0);
  284. }
  285. if (data->is_fullscreen && window->fullscreen_exclusive) {
  286. int output_width;
  287. int output_height;
  288. window_width = window->current_fullscreen_mode.w;
  289. window_height = window->current_fullscreen_mode.h;
  290. output_width = data->requested.logical_width;
  291. output_height = data->requested.logical_height;
  292. switch (GetModeScaleMethod()) {
  293. case WAYLAND_MODE_SCALE_NONE:
  294. /* The Wayland spec states that the advertised fullscreen dimensions are a maximum.
  295. * Windows can request a smaller size, but exceeding these dimensions is a protocol violation,
  296. * thus, modes that exceed the output size still need to be scaled with a viewport.
  297. */
  298. if (window_width <= output_width && window_height <= output_height) {
  299. output_width = window_width;
  300. output_height = window_height;
  301. break;
  302. }
  303. SDL_FALLTHROUGH;
  304. case WAYLAND_MODE_SCALE_ASPECT:
  305. {
  306. const float output_ratio = (float)output_width / (float)output_height;
  307. const float mode_ratio = (float)window_width / (float)window_height;
  308. if (output_ratio > mode_ratio) {
  309. output_width = SDL_lroundf((float)window_width * ((float)output_height / (float)window_height));
  310. } else if (output_ratio < mode_ratio) {
  311. output_height = SDL_lroundf((float)window_height * ((float)output_width / (float)window_width));
  312. }
  313. } break;
  314. default:
  315. break;
  316. }
  317. window_size_changed = window_width != window->w || window_height != window->h ||
  318. data->current.logical_width != output_width || data->current.logical_height != output_height;
  319. if (window_size_changed || buffer_size_changed) {
  320. if (data->viewport) {
  321. wp_viewport_set_destination(data->viewport, output_width, output_height);
  322. data->current.logical_width = output_width;
  323. data->current.logical_height = output_height;
  324. } else {
  325. // Calculate the integer scale from the mode and output.
  326. const int32_t int_scale = SDL_max(window->current_fullscreen_mode.w / output_width, 1);
  327. wl_surface_set_buffer_scale(data->surface, int_scale);
  328. data->current.logical_width = window->current_fullscreen_mode.w;
  329. data->current.logical_height = window->current_fullscreen_mode.h;
  330. }
  331. data->pointer_scale.x = (double)window_width / (double)data->current.logical_width;
  332. data->pointer_scale.y = (double)window_height / (double)data->current.logical_height;
  333. }
  334. } else {
  335. window_width = data->requested.logical_width;
  336. window_height = data->requested.logical_height;
  337. window_size_changed = window_width != data->current.logical_width || window_height != data->current.logical_height;
  338. if (window_size_changed || buffer_size_changed) {
  339. if (data->viewport) {
  340. wp_viewport_set_destination(data->viewport, window_width, window_height);
  341. } else if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
  342. // Don't change this if the DPI awareness flag is unset, as an application may have set this manually on a custom or external surface.
  343. wl_surface_set_buffer_scale(data->surface, (int32_t)scale_factor);
  344. }
  345. // Clamp the physical window size to the system minimum required size.
  346. data->current.logical_width = SDL_max(window_width, data->system_limits.min_width);
  347. data->current.logical_height = SDL_max(window_height, data->system_limits.min_height);
  348. if (!data->scale_to_display) {
  349. data->pointer_scale.x = 1.0;
  350. data->pointer_scale.y = 1.0;
  351. } else {
  352. data->pointer_scale.x = scale_factor;
  353. data->pointer_scale.y = scale_factor;
  354. }
  355. }
  356. }
  357. /*
  358. * The surface geometry, opaque region and pointer confinement region only
  359. * need to be recalculated if the output size has changed.
  360. */
  361. if (window_size_changed) {
  362. /* XXX: This is a hack and only set on the xdg-toplevel path when viewports
  363. * aren't supported to avoid a potential protocol violation if a buffer
  364. * with an old size is committed.
  365. */
  366. if (!data->viewport && data->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL && data->shell_surface.xdg.surface) {
  367. xdg_surface_set_window_geometry(data->shell_surface.xdg.surface, 0, 0, data->current.logical_width, data->current.logical_height);
  368. }
  369. SetSurfaceOpaqueRegion(data, !(window->flags & SDL_WINDOW_TRANSPARENT) && window->opacity == 1.0f);
  370. // Ensure that child popup windows are still in bounds.
  371. for (SDL_Window *child = window->first_child; child; child = child->next_sibling) {
  372. RepositionPopup(child, true);
  373. }
  374. }
  375. /* Update the min/max dimensions, primarily if the state was changed, and for non-resizable
  376. * xdg-toplevel windows where the limits should match the window size.
  377. */
  378. SetMinMaxDimensions(window);
  379. // Unconditionally send the window and drawable size, the video core will deduplicate when required.
  380. if (!data->scale_to_display) {
  381. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window_width, window_height);
  382. } else {
  383. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, data->current.pixel_width, data->current.pixel_height);
  384. }
  385. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED, data->current.pixel_width, data->current.pixel_height);
  386. /* Send an exposure event if the window is in the shown state and the size has changed,
  387. * even if the window is occluded, as the client needs to commit a new frame for the
  388. * changes to take effect.
  389. *
  390. * The occlusion state is immediately set again afterward, if necessary.
  391. */
  392. if (data->surface_status == WAYLAND_SURFACE_STATUS_SHOWN) {
  393. if ((buffer_size_changed || window_size_changed) ||
  394. (!data->suspended && (window->flags & SDL_WINDOW_OCCLUDED))) {
  395. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_EXPOSED, 0, 0);
  396. }
  397. if (data->suspended) {
  398. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_OCCLUDED, 0, 0);
  399. }
  400. }
  401. }
  402. static void CommitLibdecorFrame(SDL_Window *window)
  403. {
  404. #ifdef HAVE_LIBDECOR_H
  405. SDL_WindowData *wind = window->internal;
  406. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR && wind->shell_surface.libdecor.frame) {
  407. struct libdecor_state *state = libdecor_state_new(wind->current.logical_width, wind->current.logical_height);
  408. libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL);
  409. libdecor_state_free(state);
  410. }
  411. #endif
  412. }
  413. static void fullscreen_deadline_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
  414. {
  415. // Get the window from the ID as it may have been destroyed
  416. SDL_WindowID windowID = (SDL_WindowID)((uintptr_t)data);
  417. SDL_Window *window = SDL_GetWindowFromID(windowID);
  418. if (window && window->internal) {
  419. window->internal->fullscreen_deadline_count--;
  420. }
  421. wl_callback_destroy(callback);
  422. }
  423. static struct wl_callback_listener fullscreen_deadline_listener = {
  424. fullscreen_deadline_handler
  425. };
  426. static void maximized_deadline_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
  427. {
  428. // Get the window from the ID as it may have been destroyed
  429. SDL_WindowID windowID = (SDL_WindowID)((uintptr_t)data);
  430. SDL_Window *window = SDL_GetWindowFromID(windowID);
  431. if (window && window->internal) {
  432. window->internal->maximized_deadline_count--;
  433. }
  434. wl_callback_destroy(callback);
  435. }
  436. static struct wl_callback_listener maximized_deadline_listener = {
  437. maximized_deadline_handler
  438. };
  439. static void FlushPendingEvents(SDL_Window *window)
  440. {
  441. while (window->internal->fullscreen_deadline_count || window->internal->maximized_deadline_count) {
  442. WAYLAND_wl_display_roundtrip(window->internal->waylandData->display);
  443. }
  444. }
  445. /* While we can't get window position from the compositor, we do at least know
  446. * what monitor we're on, so let's send move events that put the window at the
  447. * center of the whatever display the wl_surface_listener events give us.
  448. */
  449. static void Wayland_move_window(SDL_Window *window)
  450. {
  451. SDL_WindowData *wind = window->internal;
  452. SDL_DisplayData *display;
  453. SDL_DisplayID *displays;
  454. if (wind->outputs && wind->num_outputs) {
  455. display = wind->outputs[wind->num_outputs - 1];
  456. } else {
  457. // A window may not be on any displays if minimized.
  458. return;
  459. }
  460. displays = SDL_GetDisplays(NULL);
  461. if (displays) {
  462. for (int i = 0; displays[i]; ++i) {
  463. if (SDL_GetDisplayDriverData(displays[i]) == display) {
  464. /* We want to send a very very specific combination here:
  465. *
  466. * 1. A coordinate that tells the application what display we're on
  467. * 2. Exactly (0, 0)
  468. *
  469. * Part 1 is useful information but is also really important for
  470. * ensuring we end up on the right display for fullscreen, while
  471. * part 2 is important because numerous applications use a specific
  472. * combination of GetWindowPosition and GetGlobalMouseState, and of
  473. * course neither are supported by Wayland. Since global mouse will
  474. * fall back to just GetMouseState, we need the window position to
  475. * be zero so the cursor math works without it going off in some
  476. * random direction. See UE5 Editor for a notable example of this!
  477. *
  478. * This may be an issue some day if we're ever able to implement
  479. * SDL_GetDisplayUsableBounds!
  480. *
  481. * -flibit
  482. */
  483. if (wind->last_displayID != displays[i]) {
  484. wind->last_displayID = displays[i];
  485. if (wind->shell_surface_type != WAYLAND_SURFACE_XDG_POPUP) {
  486. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, display->x, display->y);
  487. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_DISPLAY_CHANGED, wind->last_displayID, 0);
  488. }
  489. }
  490. break;
  491. }
  492. }
  493. SDL_free(displays);
  494. }
  495. }
  496. static void SetFullscreen(SDL_Window *window, struct wl_output *output)
  497. {
  498. SDL_WindowData *wind = window->internal;
  499. SDL_VideoData *viddata = wind->waylandData;
  500. #ifdef HAVE_LIBDECOR_H
  501. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  502. if (!wind->shell_surface.libdecor.frame) {
  503. return; // Can't do anything yet, wait for ShowWindow
  504. }
  505. wind->fullscreen_exclusive = output ? window->fullscreen_exclusive : false;
  506. ++wind->fullscreen_deadline_count;
  507. if (output) {
  508. Wayland_SetWindowResizable(SDL_GetVideoDevice(), window, true);
  509. wl_surface_commit(wind->surface);
  510. libdecor_frame_set_fullscreen(wind->shell_surface.libdecor.frame, output);
  511. } else {
  512. libdecor_frame_unset_fullscreen(wind->shell_surface.libdecor.frame);
  513. }
  514. } else
  515. #endif
  516. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
  517. if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
  518. return; // Can't do anything yet, wait for ShowWindow
  519. }
  520. wind->fullscreen_exclusive = output ? window->fullscreen_exclusive : false;
  521. ++wind->fullscreen_deadline_count;
  522. if (output) {
  523. Wayland_SetWindowResizable(SDL_GetVideoDevice(), window, true);
  524. wl_surface_commit(wind->surface);
  525. xdg_toplevel_set_fullscreen(wind->shell_surface.xdg.roleobj.toplevel, output);
  526. } else {
  527. xdg_toplevel_unset_fullscreen(wind->shell_surface.xdg.roleobj.toplevel);
  528. }
  529. }
  530. // Queue a deadline event
  531. struct wl_callback *cb = wl_display_sync(viddata->display);
  532. wl_callback_add_listener(cb, &fullscreen_deadline_listener, (void *)((uintptr_t)window->id));
  533. }
  534. static void UpdateWindowFullscreen(SDL_Window *window, bool fullscreen)
  535. {
  536. SDL_WindowData *wind = window->internal;
  537. wind->is_fullscreen = fullscreen;
  538. if (fullscreen) {
  539. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  540. SDL_copyp(&window->current_fullscreen_mode, &window->requested_fullscreen_mode);
  541. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_ENTER_FULLSCREEN, 0, 0);
  542. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_ENTER, false);
  543. /* Set the output for exclusive fullscreen windows when entering fullscreen from a
  544. * compositor event, or if the fullscreen parameters were changed between the initial
  545. * fullscreen request and now, to ensure that the window is on the correct output,
  546. * as requested by the client.
  547. */
  548. if (window->fullscreen_exclusive && (!wind->fullscreen_exclusive || !wind->fullscreen_was_positioned)) {
  549. SDL_VideoDisplay *disp = SDL_GetVideoDisplay(window->current_fullscreen_mode.displayID);
  550. if (disp) {
  551. wind->fullscreen_was_positioned = true;
  552. SetFullscreen(window, disp->internal->output);
  553. }
  554. }
  555. }
  556. } else {
  557. // Don't change the fullscreen flags if the window is hidden or being hidden.
  558. if ((window->flags & SDL_WINDOW_FULLSCREEN) && !window->is_hiding && !(window->flags & SDL_WINDOW_HIDDEN)) {
  559. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_LEAVE_FULLSCREEN, 0, 0);
  560. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, false);
  561. wind->fullscreen_was_positioned = false;
  562. /* Send a move event, in case it was deferred while the fullscreen window was moving and
  563. * on multiple outputs.
  564. */
  565. Wayland_move_window(window);
  566. }
  567. }
  568. }
  569. static const struct wl_callback_listener surface_frame_listener;
  570. static void surface_frame_done(void *data, struct wl_callback *cb, uint32_t time)
  571. {
  572. SDL_WindowData *wind = (SDL_WindowData *)data;
  573. /* XXX: This is needed to work around an Nvidia egl-wayland bug due to buffer coordinates
  574. * being used with wl_surface_damage, which causes part of the output to not be
  575. * updated when using a viewport with an output region larger than the source region.
  576. */
  577. if (wl_compositor_get_version(wind->waylandData->compositor) >= WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION) {
  578. wl_surface_damage_buffer(wind->surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
  579. } else {
  580. wl_surface_damage(wind->surface, 0, 0, SDL_MAX_SINT32, SDL_MAX_SINT32);
  581. }
  582. if (wind->surface_status == WAYLAND_SURFACE_STATUS_WAITING_FOR_FRAME) {
  583. wind->surface_status = WAYLAND_SURFACE_STATUS_SHOWN;
  584. // If any child windows are waiting on this window to be shown, show them now
  585. for (SDL_Window *w = wind->sdlwindow->first_child; w; w = w->next_sibling) {
  586. if (w->internal->surface_status == WAYLAND_SURFACE_STATUS_SHOW_PENDING) {
  587. Wayland_ShowWindow(SDL_GetVideoDevice(), w);
  588. } else if (w->internal->reparenting_required) {
  589. Wayland_SetWindowParent(SDL_GetVideoDevice(), w, w->parent);
  590. if (w->flags & SDL_WINDOW_MODAL) {
  591. Wayland_SetWindowModal(SDL_GetVideoDevice(), w, true);
  592. }
  593. }
  594. }
  595. /* If the window was initially set to the suspended state, send the occluded event now,
  596. * as we don't want to mark the window as occluded until at least one frame has been submitted.
  597. */
  598. if (wind->suspended) {
  599. SDL_SendWindowEvent(wind->sdlwindow, SDL_EVENT_WINDOW_OCCLUDED, 0, 0);
  600. }
  601. }
  602. wl_callback_destroy(cb);
  603. wind->surface_frame_callback = wl_surface_frame(wind->surface);
  604. wl_callback_add_listener(wind->surface_frame_callback, &surface_frame_listener, data);
  605. }
  606. static const struct wl_callback_listener surface_frame_listener = {
  607. surface_frame_done
  608. };
  609. static const struct wl_callback_listener gles_swap_frame_listener;
  610. static void gles_swap_frame_done(void *data, struct wl_callback *cb, uint32_t time)
  611. {
  612. SDL_WindowData *wind = (SDL_WindowData *)data;
  613. SDL_SetAtomicInt(&wind->swap_interval_ready, 1); // mark window as ready to present again.
  614. // reset this callback to fire again once a new frame was presented and compositor wants the next one.
  615. wind->gles_swap_frame_callback = wl_surface_frame(wind->gles_swap_frame_surface_wrapper);
  616. wl_callback_destroy(cb);
  617. wl_callback_add_listener(wind->gles_swap_frame_callback, &gles_swap_frame_listener, data);
  618. }
  619. static const struct wl_callback_listener gles_swap_frame_listener = {
  620. gles_swap_frame_done
  621. };
  622. static void handle_configure_xdg_shell_surface(void *data, struct xdg_surface *xdg, uint32_t serial)
  623. {
  624. SDL_WindowData *wind = (SDL_WindowData *)data;
  625. SDL_Window *window = wind->sdlwindow;
  626. ConfigureWindowGeometry(window);
  627. xdg_surface_ack_configure(xdg, serial);
  628. wind->shell_surface.xdg.initial_configure_seen = true;
  629. }
  630. static const struct xdg_surface_listener shell_surface_listener_xdg = {
  631. handle_configure_xdg_shell_surface
  632. };
  633. static void handle_configure_xdg_toplevel(void *data,
  634. struct xdg_toplevel *xdg_toplevel,
  635. int32_t width,
  636. int32_t height,
  637. struct wl_array *states)
  638. {
  639. SDL_WindowData *wind = (SDL_WindowData *)data;
  640. SDL_Window *window = wind->sdlwindow;
  641. enum xdg_toplevel_state *state;
  642. bool fullscreen = false;
  643. bool maximized = false;
  644. bool floating = true;
  645. bool tiled = false;
  646. bool active = false;
  647. bool suspended = false;
  648. wl_array_for_each (state, states) {
  649. switch (*state) {
  650. case XDG_TOPLEVEL_STATE_FULLSCREEN:
  651. fullscreen = true;
  652. floating = false;
  653. break;
  654. case XDG_TOPLEVEL_STATE_MAXIMIZED:
  655. maximized = true;
  656. floating = false;
  657. break;
  658. case XDG_TOPLEVEL_STATE_ACTIVATED:
  659. active = true;
  660. break;
  661. case XDG_TOPLEVEL_STATE_TILED_LEFT:
  662. case XDG_TOPLEVEL_STATE_TILED_RIGHT:
  663. case XDG_TOPLEVEL_STATE_TILED_TOP:
  664. case XDG_TOPLEVEL_STATE_TILED_BOTTOM:
  665. tiled = true;
  666. floating = false;
  667. break;
  668. case XDG_TOPLEVEL_STATE_SUSPENDED:
  669. suspended = true;
  670. break;
  671. default:
  672. break;
  673. }
  674. }
  675. UpdateWindowFullscreen(window, fullscreen);
  676. /* Always send a maximized/restore event; if the event is redundant it will
  677. * automatically be discarded (see src/events/SDL_windowevents.c)
  678. *
  679. * No, we do not get minimize events from xdg-shell, however, the minimized
  680. * state can be programmatically set. The meaning of 'minimized' is compositor
  681. * dependent, but in general, we can assume that the flag should remain set until
  682. * the next focused configure event occurs.
  683. */
  684. if (active || !(window->flags & SDL_WINDOW_MINIMIZED)) {
  685. if (window->flags & SDL_WINDOW_MINIMIZED) {
  686. // If we were minimized, send a restored event before possibly sending maximized.
  687. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
  688. }
  689. SDL_SendWindowEvent(window,
  690. (maximized && !fullscreen) ? SDL_EVENT_WINDOW_MAXIMIZED : SDL_EVENT_WINDOW_RESTORED,
  691. 0, 0);
  692. }
  693. if (!fullscreen) {
  694. /* xdg_toplevel spec states that this is a suggestion.
  695. * Ignore if less than or greater than max/min size.
  696. */
  697. if (window->flags & SDL_WINDOW_RESIZABLE) {
  698. if ((floating && wind->pending_restored_size) ||
  699. width == 0 || height == 0) {
  700. /* This happens when we're being restored from a non-floating state
  701. * with a pending floating client size, or the compositor indicates
  702. * that the size is up to the client, so use the cached window size here.
  703. */
  704. if (floating) {
  705. wind->pending_restored_size = false;
  706. width = window->floating.w;
  707. height = window->floating.h;
  708. } else {
  709. width = window->windowed.w;
  710. height = window->windowed.h;
  711. }
  712. if (!wind->scale_to_display) {
  713. wind->requested.logical_width = width;
  714. wind->requested.logical_height = height;
  715. } else {
  716. wind->requested.pixel_width = width;
  717. wind->requested.pixel_height = height;
  718. width = wind->requested.logical_width = PixelToPoint(window, width);
  719. height = wind->requested.logical_height = PixelToPoint(window, height);
  720. }
  721. } else {
  722. /* Don't apply the supplied dimensions if they haven't changed from the last configuration
  723. * event, or a newer size set programmatically can be overwritten by old data.
  724. */
  725. if (width != wind->last_configure.width || height != wind->last_configure.height) {
  726. wind->requested.logical_width = width;
  727. wind->requested.logical_height = height;
  728. if (wind->scale_to_display) {
  729. wind->requested.pixel_width = PointToPixel(window, width);
  730. wind->requested.pixel_height = PointToPixel(window, height);
  731. }
  732. }
  733. }
  734. } else {
  735. /* If we're a fixed-size window, we know our size for sure.
  736. * Always assume the configure is wrong.
  737. */
  738. if (!wind->scale_to_display) {
  739. width = wind->requested.logical_width = window->floating.w;
  740. height = wind->requested.logical_height = window->floating.h;
  741. } else {
  742. wind->requested.pixel_width = window->floating.w;
  743. wind->requested.pixel_height = window->floating.h;
  744. width = wind->requested.logical_width = PixelToPoint(window, window->floating.w);
  745. height = wind->requested.logical_height = PixelToPoint(window, window->floating.h);
  746. }
  747. }
  748. /* Notes on the spec:
  749. *
  750. * - The content limits are only a hint, which the compositor is free to ignore,
  751. * so apply them manually when appropriate.
  752. *
  753. * - Maximized windows must have their exact dimensions respected, thus they must
  754. * not be resized, or a protocol violation can occur.
  755. *
  756. * - When resizing a window, the width/height are maximum values, so aspect ratio
  757. * correction can't resize beyond the existing dimensions, or a protocol violation
  758. * can occur. In practice, nothing seems to kill clients that do this, but doing
  759. * so causes GNOME to glitch out.
  760. */
  761. if (!maximized) {
  762. if (!wind->scale_to_display) {
  763. if (window->max_w > 0) {
  764. wind->requested.logical_width = SDL_min(wind->requested.logical_width, window->max_w);
  765. }
  766. wind->requested.logical_width = SDL_max(wind->requested.logical_width, window->min_w);
  767. if (window->max_h > 0) {
  768. wind->requested.logical_height = SDL_min(wind->requested.logical_height, window->max_h);
  769. }
  770. wind->requested.logical_height = SDL_max(wind->requested.logical_height, window->min_h);
  771. // Aspect correction.
  772. const float aspect = (float)wind->requested.logical_width / (float)wind->requested.logical_height;
  773. if (window->min_aspect != 0.f && aspect < window->min_aspect) {
  774. wind->requested.logical_height = SDL_lroundf((float)wind->requested.logical_width / window->min_aspect);
  775. } else if (window->max_aspect != 0.f && aspect > window->max_aspect) {
  776. wind->requested.logical_width = SDL_lroundf((float)wind->requested.logical_height * window->max_aspect);
  777. }
  778. } else {
  779. if (window->max_w > 0) {
  780. wind->requested.pixel_width = SDL_min(wind->requested.pixel_width, window->max_w);
  781. }
  782. wind->requested.pixel_width = SDL_max(wind->requested.pixel_width, window->min_w);
  783. if (window->max_h > 0) {
  784. wind->requested.pixel_height = SDL_min(wind->requested.pixel_height, window->max_h);
  785. }
  786. wind->requested.pixel_height = SDL_max(wind->requested.pixel_height, window->min_h);
  787. // Aspect correction.
  788. const float aspect = (float)wind->requested.pixel_width / (float)wind->requested.pixel_height;
  789. if (window->min_aspect != 0.f && aspect < window->min_aspect) {
  790. wind->requested.pixel_height = SDL_lroundf((float)wind->requested.pixel_width / window->min_aspect);
  791. } else if (window->max_aspect != 0.f && aspect > window->max_aspect) {
  792. wind->requested.pixel_width = SDL_lroundf((float)wind->requested.pixel_height * window->max_aspect);
  793. }
  794. wind->requested.logical_width = PixelToPoint(window, wind->requested.pixel_width);
  795. wind->requested.logical_height = PixelToPoint(window, wind->requested.pixel_height);
  796. }
  797. }
  798. } else {
  799. // Fullscreen windows know their exact size.
  800. if (width == 0 || height == 0) {
  801. width = wind->requested.logical_width;
  802. height = wind->requested.logical_height;
  803. } else {
  804. wind->requested.logical_width = width;
  805. wind->requested.logical_height = height;
  806. }
  807. if (wind->scale_to_display) {
  808. wind->requested.pixel_width = PointToPixel(window, width);
  809. wind->requested.pixel_height = PointToPixel(window, height);
  810. }
  811. }
  812. wind->last_configure.width = width;
  813. wind->last_configure.height = height;
  814. wind->floating = floating;
  815. wind->suspended = suspended;
  816. wind->active = active;
  817. window->tiled = tiled;
  818. if (wind->surface_status == WAYLAND_SURFACE_STATUS_WAITING_FOR_CONFIGURE) {
  819. wind->surface_status = WAYLAND_SURFACE_STATUS_WAITING_FOR_FRAME;
  820. }
  821. }
  822. static void handle_close_xdg_toplevel(void *data, struct xdg_toplevel *xdg_toplevel)
  823. {
  824. SDL_WindowData *window = (SDL_WindowData *)data;
  825. SDL_SendWindowEvent(window->sdlwindow, SDL_EVENT_WINDOW_CLOSE_REQUESTED, 0, 0);
  826. }
  827. static void handle_xdg_configure_toplevel_bounds(void *data,
  828. struct xdg_toplevel *xdg_toplevel,
  829. int32_t width, int32_t height)
  830. {
  831. // NOP
  832. }
  833. void handle_xdg_toplevel_wm_capabilities(void *data,
  834. struct xdg_toplevel *xdg_toplevel,
  835. struct wl_array *capabilities)
  836. {
  837. // NOP
  838. }
  839. static const struct xdg_toplevel_listener toplevel_listener_xdg = {
  840. handle_configure_xdg_toplevel,
  841. handle_close_xdg_toplevel,
  842. handle_xdg_configure_toplevel_bounds, // Version 4
  843. handle_xdg_toplevel_wm_capabilities // Version 5
  844. };
  845. static void handle_configure_xdg_popup(void *data,
  846. struct xdg_popup *xdg_popup,
  847. int32_t x,
  848. int32_t y,
  849. int32_t width,
  850. int32_t height)
  851. {
  852. SDL_WindowData *wind = (SDL_WindowData *)data;
  853. int offset_x = 0, offset_y = 0;
  854. // Adjust the position if it was offset for libdecor
  855. AdjustPopupOffset(wind->sdlwindow, &offset_x, &offset_y);
  856. x -= offset_x;
  857. y -= offset_y;
  858. wind->requested.logical_width = width;
  859. wind->requested.logical_height = height;
  860. if (wind->scale_to_display) {
  861. x = PointToPixel(wind->sdlwindow->parent, x);
  862. y = PointToPixel(wind->sdlwindow->parent, y);
  863. wind->requested.pixel_width = PointToPixel(wind->sdlwindow, width);
  864. wind->requested.pixel_height = PointToPixel(wind->sdlwindow, height);
  865. }
  866. SDL_SendWindowEvent(wind->sdlwindow, SDL_EVENT_WINDOW_MOVED, x, y);
  867. if (wind->surface_status == WAYLAND_SURFACE_STATUS_WAITING_FOR_CONFIGURE) {
  868. wind->surface_status = WAYLAND_SURFACE_STATUS_WAITING_FOR_FRAME;
  869. }
  870. }
  871. static void handle_done_xdg_popup(void *data, struct xdg_popup *xdg_popup)
  872. {
  873. SDL_WindowData *window = (SDL_WindowData *)data;
  874. SDL_SendWindowEvent(window->sdlwindow, SDL_EVENT_WINDOW_CLOSE_REQUESTED, 0, 0);
  875. }
  876. static void handle_repositioned_xdg_popup(void *data,
  877. struct xdg_popup *xdg_popup,
  878. uint32_t token)
  879. {
  880. // No-op, configure does all the work we care about
  881. }
  882. static const struct xdg_popup_listener popup_listener_xdg = {
  883. handle_configure_xdg_popup,
  884. handle_done_xdg_popup,
  885. handle_repositioned_xdg_popup
  886. };
  887. static void handle_configure_zxdg_decoration(void *data,
  888. struct zxdg_toplevel_decoration_v1 *zxdg_toplevel_decoration_v1,
  889. uint32_t mode)
  890. {
  891. SDL_Window *window = (SDL_Window *)data;
  892. SDL_WindowData *internal = window->internal;
  893. SDL_VideoDevice *device = SDL_GetVideoDevice();
  894. /* If the compositor tries to force CSD anyway, bail on direct XDG support
  895. * and fall back to libdecor, it will handle these events from then on.
  896. *
  897. * To do this we have to fully unmap, then map with libdecor loaded.
  898. */
  899. if (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE) {
  900. if (window->flags & SDL_WINDOW_BORDERLESS) {
  901. // borderless windows do request CSD, so we got what we wanted
  902. return;
  903. }
  904. if (!Wayland_LoadLibdecor(internal->waylandData, true)) {
  905. // libdecor isn't available, so no borders for you... oh well
  906. return;
  907. }
  908. WAYLAND_wl_display_roundtrip(internal->waylandData->display);
  909. Wayland_HideWindow(device, window);
  910. SDL_zero(internal->shell_surface);
  911. internal->shell_surface_type = WAYLAND_SURFACE_LIBDECOR;
  912. Wayland_ShowWindow(device, window);
  913. }
  914. }
  915. static const struct zxdg_toplevel_decoration_v1_listener decoration_listener = {
  916. handle_configure_zxdg_decoration
  917. };
  918. #ifdef HAVE_LIBDECOR_H
  919. /*
  920. * XXX: Hack for older versions of libdecor that lack the function to query the
  921. * minimum content size limit. The internal limits must always be overridden
  922. * to ensure that very small windows don't cause errors or crashes.
  923. *
  924. * On libdecor >= 0.1.2, which exposes the function to get the minimum content
  925. * size limit, this function is a no-op.
  926. *
  927. * Can be removed if the minimum required version of libdecor is raised to
  928. * 0.1.2 or higher.
  929. */
  930. static void OverrideLibdecorLimits(SDL_Window *window)
  931. {
  932. #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_LIBDECOR
  933. if (!libdecor_frame_get_min_content_size) {
  934. libdecor_frame_set_min_content_size(window->internal->shell_surface.libdecor.frame, window->min_w, window->min_h);
  935. }
  936. #elif !SDL_LIBDECOR_CHECK_VERSION(0, 2, 0)
  937. libdecor_frame_set_min_content_size(window->internal->shell_surface.libdecor.frame, window->min_w, window->min_h);
  938. #endif
  939. }
  940. /*
  941. * NOTE: Retrieves the minimum content size limits, if the function for doing so is available.
  942. * On versions of libdecor that lack the minimum content size retrieval function, this
  943. * function is a no-op.
  944. *
  945. * Can be replaced with a direct call if the minimum required version of libdecor is raised
  946. * to 0.1.2 or higher.
  947. */
  948. static void LibdecorGetMinContentSize(struct libdecor_frame *frame, int *min_w, int *min_h)
  949. {
  950. #ifdef SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_LIBDECOR
  951. if (libdecor_frame_get_min_content_size != NULL) {
  952. libdecor_frame_get_min_content_size(frame, min_w, min_h);
  953. }
  954. #elif SDL_LIBDECOR_CHECK_VERSION(0, 2, 0)
  955. libdecor_frame_get_min_content_size(frame, min_w, min_h);
  956. #endif
  957. }
  958. static void decoration_frame_configure(struct libdecor_frame *frame,
  959. struct libdecor_configuration *configuration,
  960. void *user_data)
  961. {
  962. SDL_WindowData *wind = (SDL_WindowData *)user_data;
  963. SDL_Window *window = wind->sdlwindow;
  964. struct libdecor_state *state;
  965. enum libdecor_window_state window_state;
  966. int width, height;
  967. bool prev_fullscreen = wind->is_fullscreen;
  968. bool active = false;
  969. bool fullscreen = false;
  970. bool maximized = false;
  971. bool tiled = false;
  972. bool suspended = false;
  973. bool floating;
  974. static const enum libdecor_window_state tiled_states = (LIBDECOR_WINDOW_STATE_TILED_LEFT | LIBDECOR_WINDOW_STATE_TILED_RIGHT |
  975. LIBDECOR_WINDOW_STATE_TILED_TOP | LIBDECOR_WINDOW_STATE_TILED_BOTTOM);
  976. // Window State
  977. if (libdecor_configuration_get_window_state(configuration, &window_state)) {
  978. fullscreen = (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) != 0;
  979. maximized = (window_state & LIBDECOR_WINDOW_STATE_MAXIMIZED) != 0;
  980. active = (window_state & LIBDECOR_WINDOW_STATE_ACTIVE) != 0;
  981. tiled = (window_state & tiled_states) != 0;
  982. #if SDL_LIBDECOR_CHECK_VERSION(0, 2, 0)
  983. suspended = (window_state & LIBDECOR_WINDOW_STATE_SUSPENDED) != 0;
  984. #endif
  985. }
  986. floating = !(fullscreen || maximized || tiled);
  987. UpdateWindowFullscreen(window, fullscreen);
  988. /* Always send a maximized/restore event; if the event is redundant it will
  989. * automatically be discarded (see src/events/SDL_windowevents.c)
  990. *
  991. * No, we do not get minimize events from libdecor, however, the minimized
  992. * state can be programmatically set. The meaning of 'minimized' is compositor
  993. * dependent, but in general, we can assume that the flag should remain set until
  994. * the next focused configure event occurs.
  995. */
  996. if (active || !(window->flags & SDL_WINDOW_MINIMIZED)) {
  997. if (window->flags & SDL_WINDOW_MINIMIZED) {
  998. // If we were minimized, send a restored event before possibly sending maximized.
  999. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
  1000. }
  1001. SDL_SendWindowEvent(window,
  1002. (maximized && !fullscreen) ? SDL_EVENT_WINDOW_MAXIMIZED : SDL_EVENT_WINDOW_RESTORED,
  1003. 0, 0);
  1004. }
  1005. /* For fullscreen or fixed-size windows we know our size.
  1006. * Always assume the configure is wrong.
  1007. */
  1008. if (fullscreen) {
  1009. if (!libdecor_configuration_get_content_size(configuration, frame, &width, &height)) {
  1010. width = wind->requested.logical_width;
  1011. height = wind->requested.logical_height;
  1012. } else {
  1013. // Fullscreen windows know their exact size.
  1014. wind->requested.logical_width = width;
  1015. wind->requested.logical_height = height;
  1016. if (wind->scale_to_display) {
  1017. wind->requested.pixel_width = PointToPixel(window, width);
  1018. wind->requested.pixel_height = PointToPixel(window, height);
  1019. }
  1020. }
  1021. } else {
  1022. if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
  1023. /* If we're a fixed-size window, we know our size for sure.
  1024. * Always assume the configure is wrong.
  1025. */
  1026. if (!wind->scale_to_display) {
  1027. width = wind->requested.logical_width = window->floating.w;
  1028. height = wind->requested.logical_height = window->floating.h;
  1029. } else {
  1030. wind->requested.pixel_width = window->floating.w;
  1031. wind->requested.pixel_height = window->floating.h;
  1032. width = wind->requested.logical_width = PixelToPoint(window, window->floating.w);
  1033. height = wind->requested.logical_height = PixelToPoint(window, window->floating.h);
  1034. }
  1035. OverrideLibdecorLimits(window);
  1036. } else {
  1037. /* XXX: The libdecor cairo plugin sends bogus content sizes that add the
  1038. * height of the title bar when transitioning from a fixed-size to
  1039. * floating state. Ignore the sent window dimensions in this case,
  1040. * in favor of the cached value to avoid the window increasing in
  1041. * size after every state transition.
  1042. *
  1043. * https://gitlab.freedesktop.org/libdecor/libdecor/-/issues/34
  1044. */
  1045. if ((floating && (wind->pending_restored_size || (!wind->floating && !(window->flags & SDL_WINDOW_BORDERLESS)))) ||
  1046. !libdecor_configuration_get_content_size(configuration, frame, &width, &height)) {
  1047. /* This happens when we're being restored from a non-floating state,
  1048. * or the compositor indicates that the size is up to the client, so
  1049. * used the cached window size here.
  1050. */
  1051. if (floating) {
  1052. wind->pending_restored_size = false;
  1053. width = window->floating.w;
  1054. height = window->floating.h;
  1055. } else {
  1056. width = window->windowed.w;
  1057. height = window->windowed.h;
  1058. }
  1059. if (!wind->scale_to_display) {
  1060. wind->requested.logical_width = width;
  1061. wind->requested.logical_height = height;
  1062. } else {
  1063. wind->requested.pixel_width = width;
  1064. wind->requested.pixel_height = height;
  1065. width = wind->requested.logical_width = PixelToPoint(window, width);
  1066. height = wind->requested.logical_height = PixelToPoint(window, height);
  1067. }
  1068. } else {
  1069. /* Don't apply the supplied dimensions if they haven't changed from the last configuration
  1070. * event, or a newer size set programmatically can be overwritten by old data.
  1071. */
  1072. if (width != wind->last_configure.width || height != wind->last_configure.height) {
  1073. wind->requested.logical_width = width;
  1074. wind->requested.logical_height = height;
  1075. if (wind->scale_to_display) {
  1076. wind->requested.pixel_width = PointToPixel(window, width);
  1077. wind->requested.pixel_height = PointToPixel(window, height);
  1078. }
  1079. }
  1080. }
  1081. }
  1082. /* Notes on the spec:
  1083. *
  1084. * - The content limits are only a hint, which the compositor is free to ignore,
  1085. * so apply them manually when appropriate.
  1086. *
  1087. * - Maximized windows must have their exact dimensions respected, thus they must
  1088. * not be resized, or a protocol violation can occur.
  1089. *
  1090. * - When resizing a window, the width/height are maximum values, so aspect ratio
  1091. * correction can't resize beyond the existing dimensions, or a protocol violation
  1092. * can occur. In practice, nothing seems to kill clients that do this, but doing
  1093. * so causes GNOME to glitch out.
  1094. */
  1095. if (!maximized) {
  1096. if (!wind->scale_to_display) {
  1097. if (window->max_w > 0) {
  1098. wind->requested.logical_width = SDL_min(wind->requested.logical_width, window->max_w);
  1099. }
  1100. wind->requested.logical_width = SDL_max(wind->requested.logical_width, window->min_w);
  1101. if (window->max_h > 0) {
  1102. wind->requested.logical_height = SDL_min(wind->requested.logical_height, window->max_h);
  1103. }
  1104. wind->requested.logical_height = SDL_max(wind->requested.logical_height, window->min_h);
  1105. // Aspect correction.
  1106. const float aspect = (float)wind->requested.logical_width / (float)wind->requested.logical_height;
  1107. if (window->min_aspect != 0.f && aspect < window->min_aspect) {
  1108. wind->requested.logical_height = SDL_lroundf((float)wind->requested.logical_width / window->min_aspect);
  1109. } else if (window->max_aspect != 0.f && aspect > window->max_aspect) {
  1110. wind->requested.logical_width = SDL_lroundf((float)wind->requested.logical_height * window->max_aspect);
  1111. }
  1112. } else {
  1113. if (window->max_w > 0) {
  1114. wind->requested.pixel_width = SDL_min(wind->requested.pixel_width, window->max_w);
  1115. }
  1116. wind->requested.pixel_width = SDL_max(wind->requested.pixel_width, window->min_w);
  1117. if (window->max_h > 0) {
  1118. wind->requested.pixel_height = SDL_min(wind->requested.pixel_height, window->max_h);
  1119. }
  1120. wind->requested.pixel_height = SDL_max(wind->requested.pixel_height, window->min_h);
  1121. // Aspect correction.
  1122. const float aspect = (float)wind->requested.pixel_width / (float)wind->requested.pixel_height;
  1123. if (window->min_aspect != 0.f && aspect < window->min_aspect) {
  1124. wind->requested.pixel_height = SDL_lroundf((float)wind->requested.pixel_width / window->min_aspect);
  1125. } else if (window->max_aspect != 0.f && aspect > window->max_aspect) {
  1126. wind->requested.pixel_width = SDL_lroundf((float)wind->requested.pixel_height * window->max_aspect);
  1127. }
  1128. wind->requested.logical_width = PixelToPoint(window, wind->requested.pixel_width);
  1129. wind->requested.logical_height = PixelToPoint(window, wind->requested.pixel_height);
  1130. }
  1131. }
  1132. }
  1133. // Store the new state.
  1134. wind->last_configure.width = width;
  1135. wind->last_configure.height = height;
  1136. wind->floating = floating;
  1137. wind->suspended = suspended;
  1138. wind->active = active;
  1139. window->tiled = tiled;
  1140. // Calculate the new window geometry
  1141. ConfigureWindowGeometry(window);
  1142. // ... then commit the changes on the libdecor side.
  1143. state = libdecor_state_new(wind->current.logical_width, wind->current.logical_height);
  1144. libdecor_frame_commit(frame, state, configuration);
  1145. libdecor_state_free(state);
  1146. if (!wind->shell_surface.libdecor.initial_configure_seen) {
  1147. LibdecorGetMinContentSize(frame, &wind->system_limits.min_width, &wind->system_limits.min_height);
  1148. wind->shell_surface.libdecor.initial_configure_seen = true;
  1149. }
  1150. if (wind->surface_status == WAYLAND_SURFACE_STATUS_WAITING_FOR_CONFIGURE) {
  1151. wind->surface_status = WAYLAND_SURFACE_STATUS_WAITING_FOR_FRAME;
  1152. }
  1153. /* Update the resize capability if this config event was the result of the
  1154. * compositor taking a window out of fullscreen. Since this will change the
  1155. * capabilities and commit a new frame state with the last known content
  1156. * dimension, this has to be called after the new state has been committed
  1157. * and the new content dimensions were updated.
  1158. */
  1159. if (prev_fullscreen && !wind->is_fullscreen) {
  1160. Wayland_SetWindowResizable(SDL_GetVideoDevice(), window,
  1161. !!(window->flags & SDL_WINDOW_RESIZABLE));
  1162. }
  1163. }
  1164. static void decoration_frame_close(struct libdecor_frame *frame, void *user_data)
  1165. {
  1166. SDL_SendWindowEvent(((SDL_WindowData *)user_data)->sdlwindow, SDL_EVENT_WINDOW_CLOSE_REQUESTED, 0, 0);
  1167. }
  1168. static void decoration_frame_commit(struct libdecor_frame *frame, void *user_data)
  1169. {
  1170. /* libdecor decoration subsurfaces are synchronous, so the client needs to
  1171. * commit a frame to trigger an update of the decoration surfaces.
  1172. */
  1173. SDL_WindowData *wind = (SDL_WindowData *)user_data;
  1174. if (!wind->suspended && wind->surface_status == WAYLAND_SURFACE_STATUS_SHOWN) {
  1175. SDL_SendWindowEvent(wind->sdlwindow, SDL_EVENT_WINDOW_EXPOSED, 0, 0);
  1176. }
  1177. }
  1178. static struct libdecor_frame_interface libdecor_frame_interface = {
  1179. decoration_frame_configure,
  1180. decoration_frame_close,
  1181. decoration_frame_commit,
  1182. };
  1183. #endif
  1184. static void Wayland_HandlePreferredScaleChanged(SDL_WindowData *window_data, double factor)
  1185. {
  1186. const double old_factor = window_data->scale_factor;
  1187. // Round the scale factor if viewports aren't available.
  1188. if (!window_data->viewport) {
  1189. factor = SDL_ceil(factor);
  1190. }
  1191. if (factor != old_factor) {
  1192. window_data->scale_factor = factor;
  1193. if (window_data->scale_to_display) {
  1194. /* If the window is in the floating state with a user/application specified size, calculate the new
  1195. * logical size from the backbuffer size. Otherwise, use the fixed underlying logical size to calculate
  1196. * the new backbuffer dimensions.
  1197. */
  1198. if (window_data->floating) {
  1199. window_data->requested.logical_width = PixelToPoint(window_data->sdlwindow, window_data->requested.pixel_width);
  1200. window_data->requested.logical_height = PixelToPoint(window_data->sdlwindow, window_data->requested.pixel_height);
  1201. } else {
  1202. window_data->requested.pixel_width = PointToPixel(window_data->sdlwindow, window_data->requested.logical_width);
  1203. window_data->requested.pixel_height = PointToPixel(window_data->sdlwindow, window_data->requested.logical_height);
  1204. }
  1205. }
  1206. if (window_data->sdlwindow->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY || window_data->scale_to_display) {
  1207. ConfigureWindowGeometry(window_data->sdlwindow);
  1208. CommitLibdecorFrame(window_data->sdlwindow);
  1209. }
  1210. }
  1211. }
  1212. static void Wayland_MaybeUpdateScaleFactor(SDL_WindowData *window)
  1213. {
  1214. double factor;
  1215. int i;
  1216. /* If the fractional scale protocol is present or the core protocol supports the
  1217. * preferred buffer scale event, the compositor will explicitly tell the application
  1218. * what scale it wants via these events, so don't try to determine the scale factor
  1219. * from which displays the surface has entered.
  1220. */
  1221. if (window->fractional_scale || wl_surface_get_version(window->surface) >= WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION) {
  1222. return;
  1223. }
  1224. if (window->num_outputs != 0) {
  1225. // Check every display's factor, use the highest
  1226. factor = 0.0;
  1227. for (i = 0; i < window->num_outputs; i++) {
  1228. SDL_DisplayData *internal = window->outputs[i];
  1229. factor = SDL_max(factor, internal->scale_factor);
  1230. }
  1231. } else {
  1232. // All outputs removed, just fall back.
  1233. factor = window->scale_factor;
  1234. }
  1235. Wayland_HandlePreferredScaleChanged(window, factor);
  1236. }
  1237. void Wayland_RemoveOutputFromWindow(SDL_WindowData *window, SDL_DisplayData *display_data)
  1238. {
  1239. for (int i = 0; i < window->num_outputs; i++) {
  1240. if (window->outputs[i] == display_data) { // remove this one
  1241. if (i == (window->num_outputs - 1)) {
  1242. window->outputs[i] = NULL;
  1243. } else {
  1244. SDL_memmove(&window->outputs[i],
  1245. &window->outputs[i + 1],
  1246. sizeof(SDL_DisplayData *) * ((window->num_outputs - i) - 1));
  1247. }
  1248. window->num_outputs--;
  1249. i--;
  1250. }
  1251. }
  1252. if (window->num_outputs == 0) {
  1253. SDL_free(window->outputs);
  1254. window->outputs = NULL;
  1255. } else if (!window->is_fullscreen || window->num_outputs == 1) {
  1256. Wayland_move_window(window->sdlwindow);
  1257. Wayland_MaybeUpdateScaleFactor(window);
  1258. }
  1259. }
  1260. static void handle_surface_enter(void *data, struct wl_surface *surface, struct wl_output *output)
  1261. {
  1262. SDL_WindowData *window = data;
  1263. SDL_DisplayData *internal = wl_output_get_user_data(output);
  1264. SDL_DisplayData **new_outputs;
  1265. if (!SDL_WAYLAND_own_output(output) || !SDL_WAYLAND_own_surface(surface)) {
  1266. return;
  1267. }
  1268. new_outputs = SDL_realloc(window->outputs,
  1269. sizeof(SDL_DisplayData *) * (window->num_outputs + 1));
  1270. if (!new_outputs) {
  1271. return;
  1272. }
  1273. window->outputs = new_outputs;
  1274. window->outputs[window->num_outputs++] = internal;
  1275. // Update the scale factor after the move so that fullscreen outputs are updated.
  1276. if (!window->is_fullscreen || window->num_outputs == 1) {
  1277. Wayland_move_window(window->sdlwindow);
  1278. Wayland_MaybeUpdateScaleFactor(window);
  1279. }
  1280. }
  1281. static void handle_surface_leave(void *data, struct wl_surface *surface, struct wl_output *output)
  1282. {
  1283. SDL_WindowData *window = (SDL_WindowData *)data;
  1284. if (!SDL_WAYLAND_own_output(output) || !SDL_WAYLAND_own_surface(surface)) {
  1285. return;
  1286. }
  1287. Wayland_RemoveOutputFromWindow(window, (SDL_DisplayData *)wl_output_get_user_data(output));
  1288. }
  1289. static void handle_preferred_buffer_scale(void *data, struct wl_surface *wl_surface, int32_t factor)
  1290. {
  1291. SDL_WindowData *wind = data;
  1292. /* The spec is unclear on how this interacts with the fractional scaling protocol,
  1293. * so, for now, assume that the fractional scaling protocol takes priority and
  1294. * only listen to this event if the fractional scaling protocol is not present.
  1295. */
  1296. if (!wind->fractional_scale) {
  1297. Wayland_HandlePreferredScaleChanged(data, (double)factor);
  1298. }
  1299. }
  1300. static void handle_preferred_buffer_transform(void *data, struct wl_surface *wl_surface, uint32_t transform)
  1301. {
  1302. // Nothing to do here.
  1303. }
  1304. static const struct wl_surface_listener surface_listener = {
  1305. handle_surface_enter,
  1306. handle_surface_leave,
  1307. handle_preferred_buffer_scale,
  1308. handle_preferred_buffer_transform
  1309. };
  1310. static void handle_preferred_fractional_scale(void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1, uint32_t scale)
  1311. {
  1312. const double factor = (double)scale / 120.; // 120 is a magic number defined in the spec as a common denominator
  1313. Wayland_HandlePreferredScaleChanged(data, factor);
  1314. }
  1315. static const struct wp_fractional_scale_v1_listener fractional_scale_listener = {
  1316. handle_preferred_fractional_scale
  1317. };
  1318. static void frog_preferred_metadata_handler(void *data, struct frog_color_managed_surface *frog_color_managed_surface, uint32_t transfer_function,
  1319. uint32_t output_display_primary_red_x, uint32_t output_display_primary_red_y,
  1320. uint32_t output_display_primary_green_x, uint32_t output_display_primary_green_y,
  1321. uint32_t output_display_primary_blue_x, uint32_t output_display_primary_blue_y,
  1322. uint32_t output_white_point_x, uint32_t output_white_point_y,
  1323. uint32_t max_luminance, uint32_t min_luminance,
  1324. uint32_t max_full_frame_luminance)
  1325. {
  1326. SDL_WindowData *wind = (SDL_WindowData *)data;
  1327. SDL_HDROutputProperties HDR;
  1328. SDL_zero(HDR);
  1329. switch (transfer_function) {
  1330. case FROG_COLOR_MANAGED_SURFACE_TRANSFER_FUNCTION_ST2084_PQ:
  1331. /* ITU-R BT.2408-7 (Sept 2023) has the reference PQ white level at 203 nits,
  1332. * while older Dolby documentation claims a reference level of 100 nits.
  1333. *
  1334. * Use 203 nits for now.
  1335. */
  1336. HDR.HDR_headroom = max_luminance / 203.0f;
  1337. break;
  1338. case FROG_COLOR_MANAGED_SURFACE_TRANSFER_FUNCTION_SCRGB_LINEAR:
  1339. HDR.HDR_headroom = max_luminance / 80.0f;
  1340. break;
  1341. case FROG_COLOR_MANAGED_SURFACE_TRANSFER_FUNCTION_UNDEFINED:
  1342. case FROG_COLOR_MANAGED_SURFACE_TRANSFER_FUNCTION_SRGB:
  1343. case FROG_COLOR_MANAGED_SURFACE_TRANSFER_FUNCTION_GAMMA_22:
  1344. default:
  1345. HDR.HDR_headroom = 1.0f;
  1346. break;
  1347. }
  1348. HDR.SDR_white_level = 1.0f;
  1349. SDL_SetWindowHDRProperties(wind->sdlwindow, &HDR, true);
  1350. }
  1351. static const struct frog_color_managed_surface_listener frog_surface_listener = {
  1352. frog_preferred_metadata_handler
  1353. };
  1354. static void SetKeyboardFocus(SDL_Window *window)
  1355. {
  1356. SDL_Window *topmost = window;
  1357. // Find the topmost parent
  1358. while (topmost->parent) {
  1359. topmost = topmost->parent;
  1360. }
  1361. topmost->internal->keyboard_focus = window;
  1362. SDL_SetKeyboardFocus(window);
  1363. }
  1364. bool Wayland_SetWindowHitTest(SDL_Window *window, bool enabled)
  1365. {
  1366. return true; // just succeed, the real work is done elsewhere.
  1367. }
  1368. static struct xdg_toplevel *GetToplevelForWindow(SDL_WindowData *wind)
  1369. {
  1370. if (wind) {
  1371. /* Libdecor crashes on attempts to unset the parent by passing null, which is allowed by the
  1372. * toplevel spec, so just use the raw xdg-toplevel instead (that's what libdecor does
  1373. * internally anyways).
  1374. */
  1375. #ifdef HAVE_LIBDECOR_H
  1376. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR && wind->shell_surface.libdecor.frame) {
  1377. return libdecor_frame_get_xdg_toplevel(wind->shell_surface.libdecor.frame);
  1378. } else
  1379. #endif
  1380. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL && wind->shell_surface.xdg.roleobj.toplevel) {
  1381. return wind->shell_surface.xdg.roleobj.toplevel;
  1382. }
  1383. }
  1384. return NULL;
  1385. }
  1386. bool Wayland_SetWindowParent(SDL_VideoDevice *_this, SDL_Window *window, SDL_Window *parent_window)
  1387. {
  1388. SDL_WindowData *child_data = window->internal;
  1389. SDL_WindowData *parent_data = parent_window ? parent_window->internal : NULL;
  1390. child_data->reparenting_required = false;
  1391. if (parent_data && parent_data->surface_status != WAYLAND_SURFACE_STATUS_SHOWN) {
  1392. // Need to wait for the parent to become mapped, or it's the same as setting a null parent.
  1393. child_data->reparenting_required = true;
  1394. return true;
  1395. }
  1396. struct xdg_toplevel *child_toplevel = GetToplevelForWindow(child_data);
  1397. struct xdg_toplevel *parent_toplevel = GetToplevelForWindow(parent_data);
  1398. if (child_toplevel) {
  1399. xdg_toplevel_set_parent(child_toplevel, parent_toplevel);
  1400. }
  1401. return true;
  1402. }
  1403. bool Wayland_SetWindowModal(SDL_VideoDevice *_this, SDL_Window *window, bool modal)
  1404. {
  1405. SDL_VideoData *viddata = _this->internal;
  1406. SDL_WindowData *data = window->internal;
  1407. SDL_WindowData *parent_data = window->parent->internal;
  1408. if (parent_data->surface_status != WAYLAND_SURFACE_STATUS_SHOWN) {
  1409. // Need to wait for the parent to become mapped before changing modal status.
  1410. data->reparenting_required = true;
  1411. return true;
  1412. } else {
  1413. data->reparenting_required = false;
  1414. }
  1415. struct xdg_toplevel *toplevel = GetToplevelForWindow(data);
  1416. if (toplevel) {
  1417. if (viddata->xdg_wm_dialog_v1) {
  1418. if (modal) {
  1419. if (!data->xdg_dialog_v1) {
  1420. data->xdg_dialog_v1 = xdg_wm_dialog_v1_get_xdg_dialog(viddata->xdg_wm_dialog_v1, toplevel);
  1421. }
  1422. xdg_dialog_v1_set_modal(data->xdg_dialog_v1);
  1423. } else if (data->xdg_dialog_v1) {
  1424. xdg_dialog_v1_unset_modal(data->xdg_dialog_v1);
  1425. }
  1426. }
  1427. }
  1428. return true;
  1429. }
  1430. static void show_hide_sync_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
  1431. {
  1432. // Get the window from the ID as it may have been destroyed
  1433. SDL_WindowID windowID = (SDL_WindowID)((uintptr_t)data);
  1434. SDL_Window *window = SDL_GetWindowFromID(windowID);
  1435. if (window && window->internal) {
  1436. SDL_WindowData *wind = window->internal;
  1437. wind->show_hide_sync_required = false;
  1438. }
  1439. wl_callback_destroy(callback);
  1440. }
  1441. static struct wl_callback_listener show_hide_sync_listener = {
  1442. show_hide_sync_handler
  1443. };
  1444. static void exported_handle_handler(void *data, struct zxdg_exported_v2 *zxdg_exported_v2, const char *handle)
  1445. {
  1446. SDL_WindowData *wind = (SDL_WindowData*)data;
  1447. SDL_PropertiesID props = SDL_GetWindowProperties(wind->sdlwindow);
  1448. SDL_SetStringProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_TOPLEVEL_EXPORT_HANDLE_STRING, handle);
  1449. }
  1450. static struct zxdg_exported_v2_listener exported_v2_listener = {
  1451. exported_handle_handler
  1452. };
  1453. void Wayland_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1454. {
  1455. SDL_VideoData *c = _this->internal;
  1456. SDL_WindowData *data = window->internal;
  1457. SDL_PropertiesID props = SDL_GetWindowProperties(window);
  1458. // Custom surfaces don't get toplevels and are always considered 'shown'; nothing to do here.
  1459. if (data->shell_surface_type == WAYLAND_SURFACE_CUSTOM) {
  1460. return;
  1461. }
  1462. /* If this is a child window, the parent *must* be in the final shown state,
  1463. * meaning that it has received a configure event, followed by a frame callback.
  1464. * If not, a race condition can result, with effects ranging from the child
  1465. * window to spuriously closing to protocol errors.
  1466. *
  1467. * If waiting on the parent window, set the pending status and the window will
  1468. * be shown when the parent is in the shown state.
  1469. */
  1470. if (window->parent) {
  1471. if (window->parent->internal->surface_status != WAYLAND_SURFACE_STATUS_SHOWN) {
  1472. data->surface_status = WAYLAND_SURFACE_STATUS_SHOW_PENDING;
  1473. return;
  1474. }
  1475. }
  1476. /* The window was hidden, but the sync point hasn't yet been reached.
  1477. * Pump events to avoid a possible protocol violation.
  1478. */
  1479. if (data->show_hide_sync_required) {
  1480. WAYLAND_wl_display_roundtrip(c->display);
  1481. }
  1482. data->surface_status = WAYLAND_SURFACE_STATUS_WAITING_FOR_CONFIGURE;
  1483. /* Detach any previous buffers before resetting everything, otherwise when
  1484. * calling this a second time you'll get an annoying protocol error!
  1485. *
  1486. * FIXME: This was originally moved to HideWindow, which _should_ make
  1487. * sense, but for whatever reason UE5's popups require that this actually
  1488. * be in both places at once? Possibly from renderers making commits? I can't
  1489. * fully remember if this location caused crashes or if I was fixing a pair
  1490. * of Hide/Show calls. In any case, UE gives us a pretty good test and having
  1491. * both detach calls passes. This bug may be relevant if I'm wrong:
  1492. *
  1493. * https://bugs.kde.org/show_bug.cgi?id=448856
  1494. *
  1495. * -flibit
  1496. */
  1497. wl_surface_attach(data->surface, NULL, 0, 0);
  1498. wl_surface_commit(data->surface);
  1499. // Create the shell surface and map the toplevel/popup
  1500. #ifdef HAVE_LIBDECOR_H
  1501. if (data->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  1502. data->shell_surface.libdecor.frame = libdecor_decorate(c->shell.libdecor,
  1503. data->surface,
  1504. &libdecor_frame_interface,
  1505. data);
  1506. if (!data->shell_surface.libdecor.frame) {
  1507. SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Failed to create libdecor frame!");
  1508. } else {
  1509. libdecor_frame_set_app_id(data->shell_surface.libdecor.frame, data->app_id);
  1510. libdecor_frame_map(data->shell_surface.libdecor.frame);
  1511. if (c->zxdg_exporter_v2) {
  1512. data->exported = zxdg_exporter_v2_export_toplevel(c->zxdg_exporter_v2, data->surface);
  1513. zxdg_exported_v2_add_listener(data->exported, &exported_v2_listener, data);
  1514. }
  1515. if (c->xdg_toplevel_icon_manager_v1 && data->xdg_toplevel_icon_v1) {
  1516. xdg_toplevel_icon_manager_v1_set_icon(_this->internal->xdg_toplevel_icon_manager_v1,
  1517. libdecor_frame_get_xdg_toplevel(data->shell_surface.libdecor.frame),
  1518. data->xdg_toplevel_icon_v1);
  1519. }
  1520. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_SURFACE_POINTER, libdecor_frame_get_xdg_surface(data->shell_surface.libdecor.frame));
  1521. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_TOPLEVEL_POINTER, libdecor_frame_get_xdg_toplevel(data->shell_surface.libdecor.frame));
  1522. }
  1523. } else
  1524. #endif
  1525. if (data->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL || data->shell_surface_type == WAYLAND_SURFACE_XDG_POPUP) {
  1526. data->shell_surface.xdg.surface = xdg_wm_base_get_xdg_surface(c->shell.xdg, data->surface);
  1527. xdg_surface_set_user_data(data->shell_surface.xdg.surface, data);
  1528. xdg_surface_add_listener(data->shell_surface.xdg.surface, &shell_surface_listener_xdg, data);
  1529. SDL_SetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WAYLAND_XDG_SURFACE_POINTER, data->shell_surface.xdg.surface);
  1530. if (data->shell_surface_type == WAYLAND_SURFACE_XDG_POPUP) {
  1531. SDL_Window *parent = window->parent;
  1532. SDL_WindowData *parent_data = parent->internal;
  1533. struct xdg_surface *parent_xdg_surface = NULL;
  1534. int position_x = 0, position_y = 0;
  1535. // Configure the popup parameters
  1536. #ifdef HAVE_LIBDECOR_H
  1537. if (parent_data->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  1538. parent_xdg_surface = libdecor_frame_get_xdg_surface(parent_data->shell_surface.libdecor.frame);
  1539. } else
  1540. #endif
  1541. if (parent_data->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL ||
  1542. parent_data->shell_surface_type == WAYLAND_SURFACE_XDG_POPUP) {
  1543. parent_xdg_surface = parent_data->shell_surface.xdg.surface;
  1544. }
  1545. // Set up the positioner for the popup and configure the constraints
  1546. data->shell_surface.xdg.roleobj.popup.positioner = xdg_wm_base_create_positioner(c->shell.xdg);
  1547. xdg_positioner_set_anchor(data->shell_surface.xdg.roleobj.popup.positioner, XDG_POSITIONER_ANCHOR_TOP_LEFT);
  1548. xdg_positioner_set_anchor_rect(data->shell_surface.xdg.roleobj.popup.positioner, 0, 0, parent->internal->current.logical_width, parent->internal->current.logical_width);
  1549. xdg_positioner_set_constraint_adjustment(data->shell_surface.xdg.roleobj.popup.positioner,
  1550. XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y);
  1551. xdg_positioner_set_gravity(data->shell_surface.xdg.roleobj.popup.positioner, XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT);
  1552. xdg_positioner_set_size(data->shell_surface.xdg.roleobj.popup.positioner, data->current.logical_width, data->current.logical_height);
  1553. // Set the popup initial position
  1554. position_x = window->x;
  1555. position_y = window->y;
  1556. EnsurePopupPositionIsValid(window, &position_x, &position_y);
  1557. if (data->scale_to_display) {
  1558. position_x = PixelToPoint(window->parent, position_x);
  1559. position_y = PixelToPoint(window->parent, position_y);
  1560. }
  1561. AdjustPopupOffset(window, &position_x, &position_y);
  1562. xdg_positioner_set_offset(data->shell_surface.xdg.roleobj.popup.positioner, position_x, position_y);
  1563. // Assign the popup role
  1564. data->shell_surface.xdg.roleobj.popup.popup = xdg_surface_get_popup(data->shell_surface.xdg.surface,
  1565. parent_xdg_surface,
  1566. data->shell_surface.xdg.roleobj.popup.positioner);
  1567. xdg_popup_add_listener(data->shell_surface.xdg.roleobj.popup.popup, &popup_listener_xdg, data);
  1568. if (window->flags & SDL_WINDOW_TOOLTIP) {
  1569. struct wl_region *region;
  1570. // Tooltips can't be interacted with, so turn off the input region to avoid blocking anything behind them
  1571. region = wl_compositor_create_region(c->compositor);
  1572. wl_region_add(region, 0, 0, 0, 0);
  1573. wl_surface_set_input_region(data->surface, region);
  1574. wl_region_destroy(region);
  1575. } else if (window->flags & SDL_WINDOW_POPUP_MENU) {
  1576. if (window->parent == SDL_GetKeyboardFocus()) {
  1577. SetKeyboardFocus(window);
  1578. }
  1579. }
  1580. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_POPUP_POINTER, data->shell_surface.xdg.roleobj.popup.popup);
  1581. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_POSITIONER_POINTER, data->shell_surface.xdg.roleobj.popup.positioner);
  1582. } else {
  1583. data->shell_surface.xdg.roleobj.toplevel = xdg_surface_get_toplevel(data->shell_surface.xdg.surface);
  1584. xdg_toplevel_set_app_id(data->shell_surface.xdg.roleobj.toplevel, data->app_id);
  1585. xdg_toplevel_add_listener(data->shell_surface.xdg.roleobj.toplevel, &toplevel_listener_xdg, data);
  1586. if (c->zxdg_exporter_v2) {
  1587. data->exported = zxdg_exporter_v2_export_toplevel(c->zxdg_exporter_v2, data->surface);
  1588. zxdg_exported_v2_add_listener(data->exported, &exported_v2_listener, data);
  1589. }
  1590. if (c->xdg_toplevel_icon_manager_v1 && data->xdg_toplevel_icon_v1) {
  1591. xdg_toplevel_icon_manager_v1_set_icon(_this->internal->xdg_toplevel_icon_manager_v1,
  1592. data->shell_surface.xdg.roleobj.toplevel,
  1593. data->xdg_toplevel_icon_v1);
  1594. }
  1595. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_TOPLEVEL_POINTER, data->shell_surface.xdg.roleobj.toplevel);
  1596. }
  1597. }
  1598. // Restore state that was set prior to this call
  1599. Wayland_SetWindowParent(_this, window, window->parent);
  1600. if (window->flags & SDL_WINDOW_MODAL) {
  1601. Wayland_SetWindowModal(_this, window, true);
  1602. }
  1603. Wayland_SetWindowTitle(_this, window);
  1604. /* We have to wait until the surface gets a "configure" event, or use of
  1605. * this surface will fail. This is a new rule for xdg_shell.
  1606. */
  1607. #ifdef HAVE_LIBDECOR_H
  1608. if (data->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  1609. if (data->shell_surface.libdecor.frame) {
  1610. while (!data->shell_surface.libdecor.initial_configure_seen) {
  1611. WAYLAND_wl_display_flush(c->display);
  1612. WAYLAND_wl_display_dispatch(c->display);
  1613. }
  1614. }
  1615. } else
  1616. #endif
  1617. if (data->shell_surface_type == WAYLAND_SURFACE_XDG_POPUP || data->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
  1618. /* Unlike libdecor we need to call this explicitly to prevent a deadlock.
  1619. * libdecor will call this as part of their configure event!
  1620. * -flibit
  1621. */
  1622. wl_surface_commit(data->surface);
  1623. if (data->shell_surface.xdg.surface) {
  1624. while (!data->shell_surface.xdg.initial_configure_seen) {
  1625. WAYLAND_wl_display_flush(c->display);
  1626. WAYLAND_wl_display_dispatch(c->display);
  1627. }
  1628. }
  1629. // Create the window decorations
  1630. if (data->shell_surface_type != WAYLAND_SURFACE_XDG_POPUP && data->shell_surface.xdg.roleobj.toplevel && c->decoration_manager) {
  1631. data->server_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(c->decoration_manager, data->shell_surface.xdg.roleobj.toplevel);
  1632. zxdg_toplevel_decoration_v1_add_listener(data->server_decoration,
  1633. &decoration_listener,
  1634. window);
  1635. }
  1636. } else {
  1637. // Nothing to see here, just commit.
  1638. wl_surface_commit(data->surface);
  1639. }
  1640. // Make sure the window can't be resized to 0 or it can be spuriously closed by the window manager.
  1641. data->system_limits.min_width = SDL_max(data->system_limits.min_width, 1);
  1642. data->system_limits.min_height = SDL_max(data->system_limits.min_height, 1);
  1643. /* Unlike the rest of window state we have to set this _after_ flushing the
  1644. * display, because we need to create the decorations before possibly hiding
  1645. * them immediately afterward.
  1646. */
  1647. #ifdef HAVE_LIBDECOR_H
  1648. if (data->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  1649. // Libdecor plugins can enforce minimum window sizes, so adjust if the initial window size is too small.
  1650. if (window->windowed.w < data->system_limits.min_width ||
  1651. window->windowed.h < data->system_limits.min_height) {
  1652. // Warn if the window frame will be larger than the content surface.
  1653. SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO,
  1654. "Window dimensions (%i, %i) are smaller than the system enforced minimum (%i, %i); window borders will be larger than the content surface.",
  1655. window->windowed.w, window->windowed.h, data->system_limits.min_width, data->system_limits.min_height);
  1656. data->current.logical_width = SDL_max(window->windowed.w, data->system_limits.min_width);
  1657. data->current.logical_height = SDL_max(window->windowed.h, data->system_limits.min_height);
  1658. CommitLibdecorFrame(window);
  1659. }
  1660. }
  1661. #endif
  1662. Wayland_SetWindowResizable(_this, window, !!(window->flags & SDL_WINDOW_RESIZABLE));
  1663. Wayland_SetWindowBordered(_this, window, !(window->flags & SDL_WINDOW_BORDERLESS));
  1664. // We're finally done putting the window together, raise if possible
  1665. if (c->activation_manager) {
  1666. /* Note that we don't check for empty strings, as that is still
  1667. * considered a valid activation token!
  1668. */
  1669. const char *activation_token = SDL_getenv("XDG_ACTIVATION_TOKEN");
  1670. if (activation_token) {
  1671. xdg_activation_v1_activate(c->activation_manager,
  1672. activation_token,
  1673. data->surface);
  1674. // Clear this variable, per the protocol's request
  1675. SDL_unsetenv_unsafe("XDG_ACTIVATION_TOKEN");
  1676. }
  1677. }
  1678. data->show_hide_sync_required = true;
  1679. struct wl_callback *cb = wl_display_sync(_this->internal->display);
  1680. wl_callback_add_listener(cb, &show_hide_sync_listener, (void*)((uintptr_t)window->id));
  1681. // Send an exposure event to signal that the client should draw.
  1682. if (data->surface_status == WAYLAND_SURFACE_STATUS_WAITING_FOR_FRAME) {
  1683. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_EXPOSED, 0, 0);
  1684. }
  1685. }
  1686. static void Wayland_ReleasePopup(SDL_VideoDevice *_this, SDL_Window *popup)
  1687. {
  1688. SDL_WindowData *popupdata;
  1689. // Basic sanity checks to weed out the weird popup closures
  1690. if (!SDL_ObjectValid(popup, SDL_OBJECT_TYPE_WINDOW)) {
  1691. return;
  1692. }
  1693. popupdata = popup->internal;
  1694. if (!popupdata) {
  1695. return;
  1696. }
  1697. // This may already be freed by a parent popup!
  1698. if (popupdata->shell_surface.xdg.roleobj.popup.popup == NULL) {
  1699. return;
  1700. }
  1701. if (popup->flags & SDL_WINDOW_POPUP_MENU) {
  1702. if (popup == SDL_GetKeyboardFocus()) {
  1703. SDL_Window *new_focus = popup->parent;
  1704. // Find the highest level window that isn't being hidden or destroyed.
  1705. while (new_focus->parent && (new_focus->is_hiding || new_focus->is_destroying)) {
  1706. new_focus = new_focus->parent;
  1707. }
  1708. SetKeyboardFocus(new_focus);
  1709. }
  1710. }
  1711. xdg_popup_destroy(popupdata->shell_surface.xdg.roleobj.popup.popup);
  1712. xdg_positioner_destroy(popupdata->shell_surface.xdg.roleobj.popup.positioner);
  1713. popupdata->shell_surface.xdg.roleobj.popup.popup = NULL;
  1714. popupdata->shell_surface.xdg.roleobj.popup.positioner = NULL;
  1715. SDL_PropertiesID props = SDL_GetWindowProperties(popup);
  1716. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_POPUP_POINTER, NULL);
  1717. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_POSITIONER_POINTER, NULL);
  1718. }
  1719. void Wayland_HideWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1720. {
  1721. SDL_VideoData *data = _this->internal;
  1722. SDL_WindowData *wind = window->internal;
  1723. SDL_PropertiesID props = SDL_GetWindowProperties(window);
  1724. // Custom surfaces have nothing to destroy and are always considered to be 'shown'; nothing to do here.
  1725. if (wind->shell_surface_type == WAYLAND_SURFACE_CUSTOM) {
  1726. return;
  1727. }
  1728. /* The window was shown, but the sync point hasn't yet been reached.
  1729. * Pump events to avoid a possible protocol violation.
  1730. */
  1731. if (wind->show_hide_sync_required) {
  1732. WAYLAND_wl_display_roundtrip(data->display);
  1733. }
  1734. wind->surface_status = WAYLAND_SURFACE_STATUS_HIDDEN;
  1735. if (wind->server_decoration) {
  1736. zxdg_toplevel_decoration_v1_destroy(wind->server_decoration);
  1737. wind->server_decoration = NULL;
  1738. }
  1739. // Be sure to detach after this is done, otherwise ShowWindow crashes!
  1740. if (wind->shell_surface_type != WAYLAND_SURFACE_XDG_POPUP) {
  1741. wl_surface_attach(wind->surface, NULL, 0, 0);
  1742. wl_surface_commit(wind->surface);
  1743. }
  1744. // Clean up the export handle.
  1745. if (wind->exported) {
  1746. zxdg_exported_v2_destroy(wind->exported);
  1747. wind->exported = NULL;
  1748. SDL_SetStringProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_TOPLEVEL_EXPORT_HANDLE_STRING, NULL);
  1749. }
  1750. if (wind->xdg_dialog_v1) {
  1751. xdg_dialog_v1_destroy(wind->xdg_dialog_v1);
  1752. wind->xdg_dialog_v1 = NULL;
  1753. }
  1754. #ifdef HAVE_LIBDECOR_H
  1755. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  1756. if (wind->shell_surface.libdecor.frame) {
  1757. libdecor_frame_unref(wind->shell_surface.libdecor.frame);
  1758. wind->shell_surface.libdecor.frame = NULL;
  1759. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_SURFACE_POINTER, NULL);
  1760. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_TOPLEVEL_POINTER, NULL);
  1761. }
  1762. } else
  1763. #endif
  1764. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_POPUP) {
  1765. Wayland_ReleasePopup(_this, window);
  1766. } else if (wind->shell_surface.xdg.roleobj.toplevel) {
  1767. xdg_toplevel_destroy(wind->shell_surface.xdg.roleobj.toplevel);
  1768. wind->shell_surface.xdg.roleobj.toplevel = NULL;
  1769. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_TOPLEVEL_POINTER, NULL);
  1770. }
  1771. if (wind->shell_surface.xdg.surface) {
  1772. xdg_surface_destroy(wind->shell_surface.xdg.surface);
  1773. wind->shell_surface.xdg.surface = NULL;
  1774. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_SURFACE_POINTER, NULL);
  1775. }
  1776. wind->show_hide_sync_required = true;
  1777. struct wl_callback *cb = wl_display_sync(_this->internal->display);
  1778. wl_callback_add_listener(cb, &show_hide_sync_listener, (void*)((uintptr_t)window->id));
  1779. }
  1780. static void handle_xdg_activation_done(void *data,
  1781. struct xdg_activation_token_v1 *xdg_activation_token_v1,
  1782. const char *token)
  1783. {
  1784. SDL_WindowData *window = data;
  1785. if (xdg_activation_token_v1 == window->activation_token) {
  1786. xdg_activation_v1_activate(window->waylandData->activation_manager,
  1787. token,
  1788. window->surface);
  1789. xdg_activation_token_v1_destroy(window->activation_token);
  1790. window->activation_token = NULL;
  1791. }
  1792. }
  1793. static const struct xdg_activation_token_v1_listener activation_listener_xdg = {
  1794. handle_xdg_activation_done
  1795. };
  1796. /* The xdg-activation protocol considers "activation" to be one of two things:
  1797. *
  1798. * 1: Raising a window to the top and flashing the titlebar
  1799. * 2: Flashing the titlebar while keeping the window where it is
  1800. *
  1801. * As you might expect from Wayland, the general policy is to go with #2 unless
  1802. * the client can prove to the compositor beyond a reasonable doubt that raising
  1803. * the window will not be malicuous behavior.
  1804. *
  1805. * For SDL this means RaiseWindow and FlashWindow both use the same protocol,
  1806. * but in different ways: RaiseWindow will provide as _much_ information as
  1807. * possible while FlashWindow will provide as _little_ information as possible,
  1808. * to nudge the compositor into doing what we want.
  1809. *
  1810. * This isn't _strictly_ what the protocol says will happen, but this is what
  1811. * current implementations are doing (as of writing, YMMV in the far distant
  1812. * future).
  1813. *
  1814. * -flibit
  1815. */
  1816. static void Wayland_activate_window(SDL_VideoData *data, SDL_WindowData *target_wind, bool set_serial)
  1817. {
  1818. struct SDL_WaylandInput * input = data->input;
  1819. SDL_Window *focus = SDL_GetKeyboardFocus();
  1820. struct wl_surface *requesting_surface = focus ? focus->internal->surface : NULL;
  1821. if (data->activation_manager) {
  1822. if (target_wind->activation_token) {
  1823. // We're about to overwrite this with a new request
  1824. xdg_activation_token_v1_destroy(target_wind->activation_token);
  1825. }
  1826. target_wind->activation_token = xdg_activation_v1_get_activation_token(data->activation_manager);
  1827. xdg_activation_token_v1_add_listener(target_wind->activation_token,
  1828. &activation_listener_xdg,
  1829. target_wind);
  1830. /* Note that we are not setting the app_id here.
  1831. *
  1832. * Hypothetically we could set the app_id from data->classname, but
  1833. * that part of the API is for _external_ programs, not ourselves.
  1834. *
  1835. * -flibit
  1836. */
  1837. if (requesting_surface) {
  1838. // This specifies the surface from which the activation request is originating, not the activation target surface.
  1839. xdg_activation_token_v1_set_surface(target_wind->activation_token, requesting_surface);
  1840. }
  1841. if (set_serial && input && input->seat) {
  1842. xdg_activation_token_v1_set_serial(target_wind->activation_token, input->last_implicit_grab_serial, input->seat);
  1843. }
  1844. xdg_activation_token_v1_commit(target_wind->activation_token);
  1845. }
  1846. }
  1847. void Wayland_RaiseWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1848. {
  1849. Wayland_activate_window(_this->internal, window->internal, true);
  1850. }
  1851. bool Wayland_FlashWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_FlashOperation operation)
  1852. {
  1853. /* Not setting the serial will specify 'urgency' without switching focus as per
  1854. * https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/9#note_854977
  1855. */
  1856. Wayland_activate_window(_this->internal, window->internal, false);
  1857. return true;
  1858. }
  1859. SDL_FullscreenResult Wayland_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window *window,
  1860. SDL_VideoDisplay *display, SDL_FullscreenOp fullscreen)
  1861. {
  1862. SDL_WindowData *wind = window->internal;
  1863. struct wl_output *output = display->internal->output;
  1864. // Custom surfaces have no toplevel to make fullscreen.
  1865. if (wind->shell_surface_type == WAYLAND_SURFACE_CUSTOM) {
  1866. return SDL_FULLSCREEN_FAILED;
  1867. }
  1868. if (wind->show_hide_sync_required) {
  1869. WAYLAND_wl_display_roundtrip(_this->internal->display);
  1870. }
  1871. // Flushing old events pending a new one, ignore this request.
  1872. if (wind->drop_fullscreen_requests) {
  1873. return SDL_FULLSCREEN_SUCCEEDED;
  1874. }
  1875. wind->drop_fullscreen_requests = true;
  1876. FlushPendingEvents(window);
  1877. wind->drop_fullscreen_requests = false;
  1878. // Nothing to do if the window is not fullscreen, and this isn't an explicit enter request.
  1879. if (!wind->is_fullscreen) {
  1880. if (fullscreen == SDL_FULLSCREEN_OP_UPDATE) {
  1881. // Request was out of date; signal the video core not to update any state.
  1882. return SDL_FULLSCREEN_PENDING;
  1883. } else if (fullscreen == SDL_FULLSCREEN_OP_LEAVE) {
  1884. // Already not fullscreen; nothing to do.
  1885. return SDL_FULLSCREEN_SUCCEEDED;
  1886. }
  1887. }
  1888. // Don't send redundant fullscreen set/unset events.
  1889. if (fullscreen != wind->is_fullscreen) {
  1890. wind->fullscreen_was_positioned = fullscreen;
  1891. SetFullscreen(window, fullscreen ? output : NULL);
  1892. } else if (wind->is_fullscreen) {
  1893. /*
  1894. * If the window is already fullscreen, this is likely a request to switch between
  1895. * fullscreen and fullscreen desktop, change outputs, or change the video mode.
  1896. *
  1897. * If the window is already positioned on the target output, just update the
  1898. * window geometry.
  1899. */
  1900. if (wind->last_displayID != display->id) {
  1901. wind->fullscreen_was_positioned = true;
  1902. SetFullscreen(window, output);
  1903. } else {
  1904. ConfigureWindowGeometry(window);
  1905. CommitLibdecorFrame(window);
  1906. return SDL_FULLSCREEN_SUCCEEDED;
  1907. }
  1908. }
  1909. return SDL_FULLSCREEN_PENDING;
  1910. }
  1911. void Wayland_RestoreWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1912. {
  1913. SDL_WindowData *wind = window->internal;
  1914. #ifdef HAVE_LIBDECOR_H
  1915. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  1916. if (!wind->shell_surface.libdecor.frame) {
  1917. return; // Can't do anything yet, wait for ShowWindow
  1918. }
  1919. libdecor_frame_unset_maximized(wind->shell_surface.libdecor.frame);
  1920. } else
  1921. #endif
  1922. // Note that xdg-shell does NOT provide a way to unset minimize!
  1923. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
  1924. if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
  1925. return; // Can't do anything yet, wait for ShowWindow
  1926. }
  1927. xdg_toplevel_unset_maximized(wind->shell_surface.xdg.roleobj.toplevel);
  1928. }
  1929. }
  1930. void Wayland_SetWindowBordered(SDL_VideoDevice *_this, SDL_Window *window, bool bordered)
  1931. {
  1932. SDL_WindowData *wind = window->internal;
  1933. const SDL_VideoData *viddata = (const SDL_VideoData *)_this->internal;
  1934. #ifdef HAVE_LIBDECOR_H
  1935. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  1936. if (wind->shell_surface.libdecor.frame) {
  1937. libdecor_frame_set_visibility(wind->shell_surface.libdecor.frame, bordered);
  1938. }
  1939. } else
  1940. #endif
  1941. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
  1942. if ((viddata->decoration_manager) && (wind->server_decoration)) {
  1943. const enum zxdg_toplevel_decoration_v1_mode mode = bordered ? ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE : ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
  1944. zxdg_toplevel_decoration_v1_set_mode(wind->server_decoration, mode);
  1945. }
  1946. }
  1947. }
  1948. void Wayland_SetWindowResizable(SDL_VideoDevice *_this, SDL_Window *window, bool resizable)
  1949. {
  1950. #ifdef HAVE_LIBDECOR_H
  1951. const SDL_WindowData *wind = window->internal;
  1952. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  1953. if (!wind->shell_surface.libdecor.frame) {
  1954. return; // Can't do anything yet, wait for ShowWindow
  1955. }
  1956. if (libdecor_frame_has_capability(wind->shell_surface.libdecor.frame, LIBDECOR_ACTION_RESIZE)) {
  1957. if (!resizable) {
  1958. libdecor_frame_unset_capabilities(wind->shell_surface.libdecor.frame, LIBDECOR_ACTION_RESIZE);
  1959. }
  1960. } else if (resizable) {
  1961. libdecor_frame_set_capabilities(wind->shell_surface.libdecor.frame, LIBDECOR_ACTION_RESIZE);
  1962. }
  1963. }
  1964. #endif
  1965. /* When changing the resize capability on libdecor windows, the limits must always
  1966. * be reapplied, as when libdecor changes states, it overwrites the values internally.
  1967. */
  1968. SetMinMaxDimensions(window);
  1969. CommitLibdecorFrame(window);
  1970. }
  1971. void Wayland_MaximizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1972. {
  1973. SDL_VideoData *viddata = _this->internal;
  1974. SDL_WindowData *wind = window->internal;
  1975. if (wind->show_hide_sync_required) {
  1976. WAYLAND_wl_display_roundtrip(_this->internal->display);
  1977. }
  1978. #ifdef HAVE_LIBDECOR_H
  1979. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  1980. if (!wind->shell_surface.libdecor.frame) {
  1981. return; // Can't do anything yet, wait for ShowWindow
  1982. }
  1983. // Commit to preserve any pending size data.
  1984. wl_surface_commit(wind->surface);
  1985. libdecor_frame_set_maximized(wind->shell_surface.libdecor.frame);
  1986. } else
  1987. #endif
  1988. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
  1989. if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
  1990. return; // Can't do anything yet, wait for ShowWindow
  1991. }
  1992. // Commit to preserve any pending size data.
  1993. wl_surface_commit(wind->surface);
  1994. xdg_toplevel_set_maximized(wind->shell_surface.xdg.roleobj.toplevel);
  1995. }
  1996. ++wind->maximized_deadline_count;
  1997. struct wl_callback *cb = wl_display_sync(viddata->display);
  1998. wl_callback_add_listener(cb, &maximized_deadline_listener, (void *)((uintptr_t)window->id));
  1999. }
  2000. void Wayland_MinimizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2001. {
  2002. SDL_WindowData *wind = window->internal;
  2003. // TODO: Check compositor capabilities to see if minimizing is supported
  2004. #ifdef HAVE_LIBDECOR_H
  2005. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  2006. if (!wind->shell_surface.libdecor.frame) {
  2007. return; // Can't do anything yet, wait for ShowWindow
  2008. }
  2009. libdecor_frame_set_minimized(wind->shell_surface.libdecor.frame);
  2010. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MINIMIZED, 0, 0);
  2011. } else
  2012. #endif
  2013. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
  2014. if (wind->shell_surface.xdg.roleobj.toplevel == NULL) {
  2015. return; // Can't do anything yet, wait for ShowWindow
  2016. }
  2017. xdg_toplevel_set_minimized(wind->shell_surface.xdg.roleobj.toplevel);
  2018. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MINIMIZED, 0, 0);
  2019. }
  2020. }
  2021. bool Wayland_SetWindowMouseRect(SDL_VideoDevice *_this, SDL_Window *window)
  2022. {
  2023. SDL_VideoData *data = _this->internal;
  2024. /* This may look suspiciously like SetWindowGrab, despite SetMouseRect not
  2025. * implicitly doing a grab. And you're right! Wayland doesn't let us mess
  2026. * around with mouse focus whatsoever, so it just happens to be that the
  2027. * work that we can do in these two functions ends up being the same.
  2028. *
  2029. * Just know that this call lets you confine with a rect, SetWindowGrab
  2030. * lets you confine without a rect.
  2031. */
  2032. if (SDL_RectEmpty(&window->mouse_rect) && !(window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
  2033. return Wayland_input_unconfine_pointer(data->input, window);
  2034. } else {
  2035. return Wayland_input_confine_pointer(data->input, window);
  2036. }
  2037. }
  2038. bool Wayland_SetWindowMouseGrab(SDL_VideoDevice *_this, SDL_Window *window, bool grabbed)
  2039. {
  2040. SDL_VideoData *data = _this->internal;
  2041. if (grabbed) {
  2042. return Wayland_input_confine_pointer(data->input, window);
  2043. } else if (SDL_RectEmpty(&window->mouse_rect)) {
  2044. return Wayland_input_unconfine_pointer(data->input, window);
  2045. }
  2046. return true;
  2047. }
  2048. bool Wayland_SetWindowKeyboardGrab(SDL_VideoDevice *_this, SDL_Window *window, bool grabbed)
  2049. {
  2050. SDL_VideoData *data = _this->internal;
  2051. if (grabbed) {
  2052. return Wayland_input_grab_keyboard(window, data->input);
  2053. } else {
  2054. return Wayland_input_ungrab_keyboard(window);
  2055. }
  2056. }
  2057. bool Wayland_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props)
  2058. {
  2059. SDL_WindowData *data;
  2060. SDL_VideoData *c = _this->internal;
  2061. struct wl_surface *external_surface = (struct wl_surface *)SDL_GetPointerProperty(create_props, SDL_PROP_WINDOW_CREATE_WAYLAND_WL_SURFACE_POINTER,
  2062. (struct wl_surface *)SDL_GetPointerProperty(create_props, "sdl2-compat.external_window", NULL));
  2063. const bool custom_surface_role = (external_surface != NULL) || SDL_GetBooleanProperty(create_props, SDL_PROP_WINDOW_CREATE_WAYLAND_SURFACE_ROLE_CUSTOM_BOOLEAN, false);
  2064. const bool create_egl_window = !!(window->flags & SDL_WINDOW_OPENGL) ||
  2065. SDL_GetBooleanProperty(create_props, SDL_PROP_WINDOW_CREATE_WAYLAND_CREATE_EGL_WINDOW_BOOLEAN, false);
  2066. data = SDL_calloc(1, sizeof(*data));
  2067. if (!data) {
  2068. return false;
  2069. }
  2070. window->internal = data;
  2071. if (window->x == SDL_WINDOWPOS_UNDEFINED) {
  2072. window->x = 0;
  2073. }
  2074. if (window->y == SDL_WINDOWPOS_UNDEFINED) {
  2075. window->y = 0;
  2076. }
  2077. data->waylandData = c;
  2078. data->sdlwindow = window;
  2079. data->scale_factor = 1.0;
  2080. if (SDL_WINDOW_IS_POPUP(window)) {
  2081. data->scale_to_display = window->parent->internal->scale_to_display;
  2082. data->scale_factor = window->parent->internal->scale_factor;
  2083. EnsurePopupPositionIsValid(window, &window->x, &window->y);
  2084. } else {
  2085. for (int i = 0; i < _this->num_displays; i++) {
  2086. data->scale_factor = SDL_max(data->scale_factor, _this->displays[i]->internal->scale_factor);
  2087. }
  2088. }
  2089. data->outputs = NULL;
  2090. data->num_outputs = 0;
  2091. data->scale_to_display = c->scale_to_display_enabled;
  2092. // Cache the app_id at creation time, as it may change before the window is mapped.
  2093. data->app_id = SDL_strdup(SDL_GetAppID());
  2094. if (!data->scale_to_display) {
  2095. data->requested.logical_width = window->floating.w;
  2096. data->requested.logical_height = window->floating.h;
  2097. } else {
  2098. data->requested.logical_width = PixelToPoint(window, window->floating.w);
  2099. data->requested.logical_height = PixelToPoint(window, window->floating.h);
  2100. data->requested.pixel_width = window->floating.w;
  2101. data->requested.pixel_height = window->floating.h;
  2102. }
  2103. if (!external_surface) {
  2104. data->surface = wl_compositor_create_surface(c->compositor);
  2105. wl_surface_add_listener(data->surface, &surface_listener, data);
  2106. wl_surface_set_user_data(data->surface, data);
  2107. SDL_WAYLAND_register_surface(data->surface);
  2108. } else {
  2109. window->flags |= SDL_WINDOW_EXTERNAL;
  2110. data->surface = external_surface;
  2111. /* External surfaces are registered by being put in a list, as changing tags or userdata
  2112. * can cause problems with external toolkits.
  2113. */
  2114. Wayland_AddWindowDataToExternalList(data);
  2115. }
  2116. /* Always attach a viewport and fractional scale manager if available and the surface is not custom/external,
  2117. * or the custom/external surface was explicitly flagged as high pixel density aware, which signals that the
  2118. * application wants SDL to handle scaling.
  2119. */
  2120. if (!custom_surface_role || (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)) {
  2121. if (c->viewporter) {
  2122. data->viewport = wp_viewporter_get_viewport(c->viewporter, data->surface);
  2123. // The viewport always uses the entire buffer.
  2124. wp_viewport_set_source(data->viewport,
  2125. wl_fixed_from_int(-1), wl_fixed_from_int(-1),
  2126. wl_fixed_from_int(-1), wl_fixed_from_int(-1));
  2127. }
  2128. if (c->fractional_scale_manager) {
  2129. data->fractional_scale = wp_fractional_scale_manager_v1_get_fractional_scale(c->fractional_scale_manager, data->surface);
  2130. wp_fractional_scale_v1_add_listener(data->fractional_scale, &fractional_scale_listener, data);
  2131. }
  2132. }
  2133. if (!custom_surface_role) {
  2134. if (c->frog_color_management_factory_v1) {
  2135. data->frog_color_managed_surface = frog_color_management_factory_v1_get_color_managed_surface(c->frog_color_management_factory_v1, data->surface);
  2136. frog_color_managed_surface_add_listener(data->frog_color_managed_surface, &frog_surface_listener, data);
  2137. }
  2138. if (c->wp_alpha_modifier_v1) {
  2139. data->wp_alpha_modifier_surface_v1 = wp_alpha_modifier_v1_get_surface(c->wp_alpha_modifier_v1, data->surface);
  2140. wp_alpha_modifier_surface_v1_set_multiplier(data->wp_alpha_modifier_surface_v1, SDL_MAX_UINT32);
  2141. }
  2142. }
  2143. // Must be called before EGL configuration to set the drawable backbuffer size.
  2144. ConfigureWindowGeometry(window);
  2145. /* Fire a callback when the compositor wants a new frame rendered.
  2146. * Right now this only matters for OpenGL; we use this callback to add a
  2147. * wait timeout that avoids getting deadlocked by the compositor when the
  2148. * window isn't visible.
  2149. */
  2150. if (window->flags & SDL_WINDOW_OPENGL) {
  2151. data->gles_swap_frame_event_queue = WAYLAND_wl_display_create_queue(data->waylandData->display);
  2152. data->gles_swap_frame_surface_wrapper = WAYLAND_wl_proxy_create_wrapper(data->surface);
  2153. WAYLAND_wl_proxy_set_queue((struct wl_proxy *)data->gles_swap_frame_surface_wrapper, data->gles_swap_frame_event_queue);
  2154. data->gles_swap_frame_callback = wl_surface_frame(data->gles_swap_frame_surface_wrapper);
  2155. wl_callback_add_listener(data->gles_swap_frame_callback, &gles_swap_frame_listener, data);
  2156. }
  2157. // No frame callback on external surfaces as it may already have one attached.
  2158. if (!external_surface) {
  2159. // Fire a callback when the compositor wants a new frame to set the surface damage region.
  2160. data->surface_frame_callback = wl_surface_frame(data->surface);
  2161. wl_callback_add_listener(data->surface_frame_callback, &surface_frame_listener, data);
  2162. }
  2163. if (window->flags & SDL_WINDOW_TRANSPARENT) {
  2164. if (_this->gl_config.alpha_size == 0) {
  2165. _this->gl_config.alpha_size = 8;
  2166. }
  2167. }
  2168. if (create_egl_window) {
  2169. data->egl_window = WAYLAND_wl_egl_window_create(data->surface, data->current.pixel_width, data->current.pixel_height);
  2170. }
  2171. #ifdef SDL_VIDEO_OPENGL_EGL
  2172. if (window->flags & SDL_WINDOW_OPENGL) {
  2173. // Create the GLES window surface
  2174. data->egl_surface = SDL_EGL_CreateSurface(_this, window, (NativeWindowType)data->egl_window);
  2175. if (data->egl_surface == EGL_NO_SURFACE) {
  2176. return false; // SDL_EGL_CreateSurface should have set error
  2177. }
  2178. }
  2179. #endif
  2180. if (c->relative_mouse_mode) {
  2181. Wayland_input_enable_relative_pointer(c->input);
  2182. }
  2183. // We may need to create an idle inhibitor for this new window
  2184. Wayland_SuspendScreenSaver(_this);
  2185. if (!custom_surface_role) {
  2186. #ifdef HAVE_LIBDECOR_H
  2187. if (c->shell.libdecor && !SDL_WINDOW_IS_POPUP(window)) {
  2188. data->shell_surface_type = WAYLAND_SURFACE_LIBDECOR;
  2189. } else
  2190. #endif
  2191. if (c->shell.xdg) {
  2192. if (SDL_WINDOW_IS_POPUP(window)) {
  2193. data->shell_surface_type = WAYLAND_SURFACE_XDG_POPUP;
  2194. } else {
  2195. data->shell_surface_type = WAYLAND_SURFACE_XDG_TOPLEVEL;
  2196. }
  2197. } // All other cases will be WAYLAND_SURFACE_UNKNOWN
  2198. } else {
  2199. // Roleless and external surfaces are always considered to be in the shown state by the backend.
  2200. data->shell_surface_type = WAYLAND_SURFACE_CUSTOM;
  2201. data->surface_status = WAYLAND_SURFACE_STATUS_SHOWN;
  2202. }
  2203. if (SDL_GetHintBoolean(SDL_HINT_VIDEO_DOUBLE_BUFFER, false)) {
  2204. data->double_buffer = true;
  2205. }
  2206. SDL_PropertiesID props = SDL_GetWindowProperties(window);
  2207. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, data->waylandData->display);
  2208. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, data->surface);
  2209. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_EGL_WINDOW_POINTER, data->egl_window);
  2210. data->hit_test_result = SDL_HITTEST_NORMAL;
  2211. return true;
  2212. }
  2213. void Wayland_SetWindowMinimumSize(SDL_VideoDevice *_this, SDL_Window *window)
  2214. {
  2215. // Will be committed when Wayland_SetWindowSize() is called by the video core.
  2216. SetMinMaxDimensions(window);
  2217. }
  2218. void Wayland_SetWindowMaximumSize(SDL_VideoDevice *_this, SDL_Window *window)
  2219. {
  2220. // Will be committed when Wayland_SetWindowSize() is called by the video core.
  2221. SetMinMaxDimensions(window);
  2222. }
  2223. bool Wayland_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window)
  2224. {
  2225. SDL_WindowData *wind = window->internal;
  2226. // Only popup windows can be positioned relative to the parent.
  2227. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_POPUP) {
  2228. if (wind->shell_surface.xdg.roleobj.popup.popup &&
  2229. xdg_popup_get_version(wind->shell_surface.xdg.roleobj.popup.popup) < XDG_POPUP_REPOSITION_SINCE_VERSION) {
  2230. return SDL_Unsupported();
  2231. }
  2232. RepositionPopup(window, false);
  2233. return true;
  2234. } else if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR || wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
  2235. const int x = window->floating.x;
  2236. const int y = window->floating.y;
  2237. /* Catch up on any pending state before attempting to change the fullscreen window
  2238. * display via a set fullscreen call to make sure the window doesn't have a pending
  2239. * leave fullscreen event that it might override.
  2240. */
  2241. FlushPendingEvents(window);
  2242. /* XXX: Need to restore this after the roundtrip, as the requested coordinates might
  2243. * have been overwritten by the 'real' coordinates if a display enter/leave event
  2244. * occurred.
  2245. *
  2246. * The common pattern:
  2247. *
  2248. * SDL_SetWindowPosition();
  2249. * SDL_SetWindowFullscreen();
  2250. *
  2251. * for positioning a desktop fullscreen window won't work without this.
  2252. */
  2253. window->floating.x = x;
  2254. window->floating.y = y;
  2255. if (wind->is_fullscreen) {
  2256. SDL_VideoDisplay *display = SDL_GetVideoDisplayForFullscreenWindow(window);
  2257. if (display && wind->last_displayID != display->id) {
  2258. struct wl_output *output = display->internal->output;
  2259. SetFullscreen(window, output);
  2260. return true;
  2261. }
  2262. }
  2263. }
  2264. return SDL_SetError("wayland cannot position non-popup windows");
  2265. }
  2266. void Wayland_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
  2267. {
  2268. SDL_WindowData *wind = window->internal;
  2269. /* Fullscreen windows do not get explicitly resized, and not strictly
  2270. * obeying the size of maximized windows is a protocol violation, so
  2271. * it is necessary to flush any of these pending state operations.
  2272. *
  2273. * Calling this on a custom surface is informative, so the size must
  2274. * always be passed through.
  2275. */
  2276. FlushPendingEvents(window);
  2277. if (!(window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_MAXIMIZED)) ||
  2278. wind->shell_surface_type == WAYLAND_SURFACE_CUSTOM) {
  2279. if (!wind->scale_to_display) {
  2280. wind->requested.logical_width = window->floating.w;
  2281. wind->requested.logical_height = window->floating.h;
  2282. } else {
  2283. wind->requested.logical_width = PixelToPoint(window, window->floating.w);
  2284. wind->requested.logical_height = PixelToPoint(window, window->floating.h);
  2285. wind->requested.pixel_width = window->floating.w;
  2286. wind->requested.pixel_height = window->floating.h;
  2287. }
  2288. ConfigureWindowGeometry(window);
  2289. } else {
  2290. wind->pending_restored_size = true;
  2291. }
  2292. // Always commit, as this may be in response to a min/max limit change.
  2293. CommitLibdecorFrame(window);
  2294. }
  2295. void Wayland_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h)
  2296. {
  2297. SDL_WindowData *data = window->internal;
  2298. *w = data->current.pixel_width;
  2299. *h = data->current.pixel_height;
  2300. }
  2301. SDL_DisplayID Wayland_GetDisplayForWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2302. {
  2303. SDL_WindowData *wind = window->internal;
  2304. if (wind) {
  2305. return wind->last_displayID;
  2306. }
  2307. return 0;
  2308. }
  2309. bool Wayland_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float opacity)
  2310. {
  2311. SDL_WindowData *wind = window->internal;
  2312. if (wind->wp_alpha_modifier_surface_v1) {
  2313. SetSurfaceOpaqueRegion(wind, !(window->flags & SDL_WINDOW_TRANSPARENT) && opacity == 1.0f);
  2314. wp_alpha_modifier_surface_v1_set_multiplier(wind->wp_alpha_modifier_surface_v1, (Uint32)((double)SDL_MAX_UINT32 * (double)opacity));
  2315. return true;
  2316. }
  2317. return SDL_SetError("wayland: set window opacity failed; compositor lacks support for the required wp_alpha_modifier_v1 protocol");
  2318. }
  2319. void Wayland_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window)
  2320. {
  2321. SDL_WindowData *wind = window->internal;
  2322. const char *title = window->title ? window->title : "";
  2323. #ifdef HAVE_LIBDECOR_H
  2324. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR && wind->shell_surface.libdecor.frame) {
  2325. libdecor_frame_set_title(wind->shell_surface.libdecor.frame, title);
  2326. } else
  2327. #endif
  2328. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL && wind->shell_surface.xdg.roleobj.toplevel) {
  2329. xdg_toplevel_set_title(wind->shell_surface.xdg.roleobj.toplevel, title);
  2330. }
  2331. }
  2332. bool Wayland_SetWindowIcon(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surface *icon)
  2333. {
  2334. SDL_WindowData *wind = window->internal;
  2335. struct xdg_toplevel *toplevel = NULL;
  2336. if (!_this->internal->xdg_toplevel_icon_manager_v1) {
  2337. return SDL_SetError("wayland: cannot set icon; xdg_toplevel_icon_v1 protocol not supported");
  2338. }
  2339. if (icon->w != icon->h) {
  2340. return SDL_SetError("wayland: icon width and height must be equal, got %ix%i", icon->w, icon->h);
  2341. }
  2342. if (wind->xdg_toplevel_icon_v1) {
  2343. xdg_toplevel_icon_v1_destroy(wind->xdg_toplevel_icon_v1);
  2344. wind->xdg_toplevel_icon_v1 = NULL;
  2345. }
  2346. // TODO: Add high-DPI icon support
  2347. Wayland_ReleaseSHMBuffer(&wind->icon);
  2348. if (Wayland_AllocSHMBuffer(icon->w, icon->h, &wind->icon) != 0) {
  2349. return SDL_SetError("wayland: failed to allocate SHM buffer for the icon");
  2350. }
  2351. SDL_PremultiplyAlpha(icon->w, icon->h, icon->format, icon->pixels, icon->pitch, SDL_PIXELFORMAT_ARGB8888, wind->icon.shm_data, icon->w * 4, true);
  2352. wind->xdg_toplevel_icon_v1 = xdg_toplevel_icon_manager_v1_create_icon(_this->internal->xdg_toplevel_icon_manager_v1);
  2353. xdg_toplevel_icon_v1_add_buffer(wind->xdg_toplevel_icon_v1, wind->icon.wl_buffer, 1);
  2354. #ifdef HAVE_LIBDECOR_H
  2355. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR && wind->shell_surface.libdecor.frame) {
  2356. toplevel = libdecor_frame_get_xdg_toplevel(wind->shell_surface.libdecor.frame);
  2357. } else
  2358. #endif
  2359. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL && wind->shell_surface.xdg.roleobj.toplevel) {
  2360. toplevel = wind->shell_surface.xdg.roleobj.toplevel;
  2361. }
  2362. if (toplevel) {
  2363. xdg_toplevel_icon_manager_v1_set_icon(_this->internal->xdg_toplevel_icon_manager_v1, toplevel, wind->xdg_toplevel_icon_v1);
  2364. }
  2365. return true;
  2366. }
  2367. bool Wayland_SyncWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2368. {
  2369. SDL_WindowData *wind = window->internal;
  2370. do {
  2371. WAYLAND_wl_display_roundtrip(_this->internal->display);
  2372. } while (wind->fullscreen_deadline_count || wind->maximized_deadline_count);
  2373. return true;
  2374. }
  2375. void Wayland_ShowWindowSystemMenu(SDL_Window *window, int x, int y)
  2376. {
  2377. SDL_WindowData *wind = window->internal;
  2378. if (wind->scale_to_display) {
  2379. x = PixelToPoint(window, x);
  2380. y = PixelToPoint(window, y);
  2381. }
  2382. #ifdef HAVE_LIBDECOR_H
  2383. if (wind->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
  2384. if (wind->shell_surface.libdecor.frame) {
  2385. libdecor_frame_show_window_menu(wind->shell_surface.libdecor.frame, wind->waylandData->input->seat, wind->waylandData->input->last_implicit_grab_serial, x, y);
  2386. }
  2387. } else
  2388. #endif
  2389. if (wind->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
  2390. if (wind->shell_surface.xdg.roleobj.toplevel) {
  2391. xdg_toplevel_show_window_menu(wind->shell_surface.xdg.roleobj.toplevel, wind->waylandData->input->seat, wind->waylandData->input->last_implicit_grab_serial, x, y);
  2392. }
  2393. }
  2394. }
  2395. bool Wayland_SuspendScreenSaver(SDL_VideoDevice *_this)
  2396. {
  2397. SDL_VideoData *data = _this->internal;
  2398. #ifdef SDL_USE_LIBDBUS
  2399. if (SDL_DBus_ScreensaverInhibit(_this->suspend_screensaver)) {
  2400. return true;
  2401. }
  2402. #endif
  2403. /* The idle_inhibit_unstable_v1 protocol suspends the screensaver
  2404. on a per wl_surface basis, but SDL assumes that suspending
  2405. the screensaver can be done independently of any window.
  2406. To reconcile these differences, we propagate the idle inhibit
  2407. state to each window. If there is no window active, we will
  2408. be able to inhibit idle once the first window is created.
  2409. */
  2410. if (data->idle_inhibit_manager) {
  2411. SDL_Window *window = _this->windows;
  2412. while (window) {
  2413. SDL_WindowData *win_data = window->internal;
  2414. if (_this->suspend_screensaver && !win_data->idle_inhibitor) {
  2415. win_data->idle_inhibitor =
  2416. zwp_idle_inhibit_manager_v1_create_inhibitor(data->idle_inhibit_manager,
  2417. win_data->surface);
  2418. } else if (!_this->suspend_screensaver && win_data->idle_inhibitor) {
  2419. zwp_idle_inhibitor_v1_destroy(win_data->idle_inhibitor);
  2420. win_data->idle_inhibitor = NULL;
  2421. }
  2422. window = window->next;
  2423. }
  2424. }
  2425. return true;
  2426. }
  2427. void Wayland_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2428. {
  2429. SDL_VideoData *data = _this->internal;
  2430. SDL_WindowData *wind = window->internal;
  2431. if (data && wind) {
  2432. /* Roundtrip before destroying the window to make sure that it has received input leave events, so that
  2433. * no internal structures are left pointing to the destroyed window.
  2434. */
  2435. if (wind->show_hide_sync_required) {
  2436. WAYLAND_wl_display_roundtrip(data->display);
  2437. }
  2438. #ifdef SDL_VIDEO_OPENGL_EGL
  2439. if (wind->egl_surface) {
  2440. SDL_EGL_DestroySurface(_this, wind->egl_surface);
  2441. }
  2442. #endif
  2443. if (wind->egl_window) {
  2444. WAYLAND_wl_egl_window_destroy(wind->egl_window);
  2445. }
  2446. if (wind->idle_inhibitor) {
  2447. zwp_idle_inhibitor_v1_destroy(wind->idle_inhibitor);
  2448. }
  2449. if (wind->activation_token) {
  2450. xdg_activation_token_v1_destroy(wind->activation_token);
  2451. }
  2452. if (wind->viewport) {
  2453. wp_viewport_destroy(wind->viewport);
  2454. }
  2455. if (wind->fractional_scale) {
  2456. wp_fractional_scale_v1_destroy(wind->fractional_scale);
  2457. }
  2458. if (wind->wp_alpha_modifier_surface_v1) {
  2459. wp_alpha_modifier_surface_v1_destroy(wind->wp_alpha_modifier_surface_v1);
  2460. }
  2461. if (wind->frog_color_managed_surface) {
  2462. frog_color_managed_surface_destroy(wind->frog_color_managed_surface);
  2463. }
  2464. SDL_free(wind->outputs);
  2465. SDL_free(wind->app_id);
  2466. if (wind->gles_swap_frame_callback) {
  2467. wl_callback_destroy(wind->gles_swap_frame_callback);
  2468. WAYLAND_wl_proxy_wrapper_destroy(wind->gles_swap_frame_surface_wrapper);
  2469. WAYLAND_wl_event_queue_destroy(wind->gles_swap_frame_event_queue);
  2470. }
  2471. if (wind->surface_frame_callback) {
  2472. wl_callback_destroy(wind->surface_frame_callback);
  2473. }
  2474. if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
  2475. wl_surface_destroy(wind->surface);
  2476. } else {
  2477. Wayland_RemoveWindowDataFromExternalList(wind);
  2478. }
  2479. if (wind->xdg_toplevel_icon_v1) {
  2480. xdg_toplevel_icon_v1_destroy(wind->xdg_toplevel_icon_v1);
  2481. }
  2482. Wayland_ReleaseSHMBuffer(&wind->icon);
  2483. SDL_free(wind);
  2484. WAYLAND_wl_display_flush(data->display);
  2485. }
  2486. window->internal = NULL;
  2487. }
  2488. #endif // SDL_VIDEO_DRIVER_WAYLAND