SDL_waylandwindow.c 137 KB

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