wl_window.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923
  1. //========================================================================
  2. // GLFW 3.4 Wayland - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2014 Jonas Ådahl <jadahl@gmail.com>
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would
  17. // be appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not
  20. // be misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. //
  25. //========================================================================
  26. // It is fine to use C99 in this file because it will not be built with VS
  27. //========================================================================
  28. #define _GNU_SOURCE
  29. #include "internal.h"
  30. #include "backend_utils.h"
  31. #include "linux_notify.h"
  32. #include "wl_client_side_decorations.h"
  33. #include "../kitty/monotonic.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <fcntl.h>
  39. #include <sys/mman.h>
  40. #define debug debug_rendering
  41. static bool
  42. is_layer_shell(_GLFWwindow *window) { return window->wl.layer_shell.config.type != GLFW_LAYER_SHELL_NONE; }
  43. static void
  44. activation_token_done(void *data, struct xdg_activation_token_v1 *xdg_token, const char *token) {
  45. for (size_t i = 0; i < _glfw.wl.activation_requests.sz; i++) {
  46. glfw_wl_xdg_activation_request *r = _glfw.wl.activation_requests.array + i;
  47. if (r->request_id == (uintptr_t)data) {
  48. _GLFWwindow *window = _glfwWindowForId(r->window_id);
  49. if (r->callback) r->callback((GLFWwindow*)window, token, r->callback_data);
  50. remove_i_from_array(_glfw.wl.activation_requests.array, i, _glfw.wl.activation_requests.sz);
  51. break;
  52. }
  53. }
  54. xdg_activation_token_v1_destroy(xdg_token);
  55. }
  56. static const struct
  57. xdg_activation_token_v1_listener activation_token_listener = {
  58. .done = &activation_token_done,
  59. };
  60. static bool
  61. get_activation_token(
  62. _GLFWwindow *window, uint32_t serial, GLFWactivationcallback cb, void *cb_data
  63. ) {
  64. #define fail(msg) { _glfwInputError(GLFW_PLATFORM_ERROR, msg); if (cb) cb((GLFWwindow*)window, NULL, cb_data); return false; }
  65. if (_glfw.wl.xdg_activation_v1 == NULL) fail("Wayland: activation requests not supported by this Wayland compositor");
  66. struct xdg_activation_token_v1 *token = xdg_activation_v1_get_activation_token(_glfw.wl.xdg_activation_v1);
  67. if (token == NULL) fail("Wayland: failed to create activation request token");
  68. if (_glfw.wl.activation_requests.capacity < _glfw.wl.activation_requests.sz + 1) {
  69. _glfw.wl.activation_requests.capacity = MAX(64u, _glfw.wl.activation_requests.capacity * 2);
  70. _glfw.wl.activation_requests.array = realloc(_glfw.wl.activation_requests.array, _glfw.wl.activation_requests.capacity * sizeof(_glfw.wl.activation_requests.array[0]));
  71. if (!_glfw.wl.activation_requests.array) {
  72. _glfw.wl.activation_requests.capacity = 0;
  73. fail("Wayland: Out of memory while allocation activation request");
  74. }
  75. }
  76. glfw_wl_xdg_activation_request *r = _glfw.wl.activation_requests.array + _glfw.wl.activation_requests.sz++;
  77. memset(r, 0, sizeof(*r));
  78. static uintptr_t rq = 0;
  79. r->window_id = window->id;
  80. r->callback = cb; r->callback_data = cb_data;
  81. r->request_id = ++rq; r->token = token;
  82. if (serial != 0)
  83. xdg_activation_token_v1_set_serial(token, serial, _glfw.wl.seat);
  84. xdg_activation_token_v1_set_surface(token, window->wl.surface);
  85. xdg_activation_token_v1_add_listener(token, &activation_token_listener, (void*)r->request_id);
  86. xdg_activation_token_v1_commit(token);
  87. return true;
  88. #undef fail
  89. }
  90. static void
  91. convert_glfw_image_to_wayland_image(const GLFWimage* image, unsigned char *target) {
  92. // convert RGBA non-premultiplied to ARGB pre-multiplied
  93. unsigned char* source = (unsigned char*) image->pixels;
  94. for (int i = 0; i < image->width * image->height; i++, source += 4) {
  95. unsigned int alpha = source[3];
  96. *target++ = (unsigned char) ((source[2] * alpha) / 255);
  97. *target++ = (unsigned char) ((source[1] * alpha) / 255);
  98. *target++ = (unsigned char) ((source[0] * alpha) / 255);
  99. *target++ = (unsigned char) alpha;
  100. }
  101. }
  102. static struct wl_buffer* createShmBuffer(const GLFWimage* image, bool is_opaque, bool init_data)
  103. {
  104. struct wl_shm_pool* pool;
  105. struct wl_buffer* buffer;
  106. int stride = image->width * 4;
  107. int length = image->width * image->height * 4;
  108. void* data;
  109. int fd;
  110. fd = createAnonymousFile(length);
  111. if (fd < 0)
  112. {
  113. _glfwInputError(GLFW_PLATFORM_ERROR,
  114. "Wayland: Creating a buffer file for %d B failed: %s",
  115. length, strerror(errno));
  116. return NULL;
  117. }
  118. data = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  119. if (data == MAP_FAILED)
  120. {
  121. _glfwInputError(GLFW_PLATFORM_ERROR,
  122. "Wayland: mmap failed: %s", strerror(errno));
  123. close(fd);
  124. return NULL;
  125. }
  126. pool = wl_shm_create_pool(_glfw.wl.shm, fd, length);
  127. close(fd);
  128. if (init_data) convert_glfw_image_to_wayland_image(image, data);
  129. buffer =
  130. wl_shm_pool_create_buffer(pool, 0,
  131. image->width,
  132. image->height,
  133. stride, is_opaque ? WL_SHM_FORMAT_XRGB8888 : WL_SHM_FORMAT_ARGB8888);
  134. munmap(data, length);
  135. wl_shm_pool_destroy(pool);
  136. return buffer;
  137. }
  138. wayland_cursor_shape
  139. glfw_cursor_shape_to_wayland_cursor_shape(GLFWCursorShape g) {
  140. wayland_cursor_shape ans = {-1, ""};
  141. #define C(g, w) case g: ans.which = w; ans.name = #w; return ans;
  142. switch(g) {
  143. /* start glfw to wayland mapping (auto generated by gen-key-constants.py do not edit) */
  144. C(GLFW_DEFAULT_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
  145. C(GLFW_TEXT_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT);
  146. C(GLFW_POINTER_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER);
  147. C(GLFW_HELP_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_HELP);
  148. C(GLFW_WAIT_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_WAIT);
  149. C(GLFW_PROGRESS_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_PROGRESS);
  150. C(GLFW_CROSSHAIR_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR);
  151. C(GLFW_CELL_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CELL);
  152. C(GLFW_VERTICAL_TEXT_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_VERTICAL_TEXT);
  153. C(GLFW_MOVE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE);
  154. C(GLFW_E_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_E_RESIZE);
  155. C(GLFW_NE_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NE_RESIZE);
  156. C(GLFW_NW_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NW_RESIZE);
  157. C(GLFW_N_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_N_RESIZE);
  158. C(GLFW_SE_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE);
  159. C(GLFW_SW_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SW_RESIZE);
  160. C(GLFW_S_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_S_RESIZE);
  161. C(GLFW_W_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_W_RESIZE);
  162. C(GLFW_EW_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_EW_RESIZE);
  163. C(GLFW_NS_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NS_RESIZE);
  164. C(GLFW_NESW_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE);
  165. C(GLFW_NWSE_RESIZE_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE);
  166. C(GLFW_ZOOM_IN_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_IN);
  167. C(GLFW_ZOOM_OUT_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_OUT);
  168. C(GLFW_ALIAS_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALIAS);
  169. C(GLFW_COPY_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COPY);
  170. C(GLFW_NOT_ALLOWED_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NOT_ALLOWED);
  171. C(GLFW_NO_DROP_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NO_DROP);
  172. C(GLFW_GRAB_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRAB);
  173. C(GLFW_GRABBING_CURSOR, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING);
  174. /* end glfw to wayland mapping */
  175. default: return ans;
  176. }
  177. #undef C
  178. }
  179. static void
  180. commit_window_surface(_GLFWwindow *window) {
  181. // debug("Window %llu surface committed\n", window->id); dont log as every frame request causes a surface commit
  182. wl_surface_commit(window->wl.surface);
  183. }
  184. static void
  185. commit_window_surface_if_safe(_GLFWwindow *window) {
  186. // we only commit if the buffer attached to the surface is the correct size,
  187. // which means that at least one frame is drawn after resizeFramebuffer()
  188. if (!window->wl.waiting_for_swap_to_commit) commit_window_surface(window);
  189. }
  190. static void
  191. set_cursor_surface(struct wl_surface *surface, int hotspot_x, int hotspot_y, const char *from_where) {
  192. debug("Calling wl_pointer_set_cursor in %s with surface: %p and serial: %u\n", from_where, (void*)surface, _glfw.wl.pointer_enter_serial);
  193. wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.pointer_enter_serial, surface, hotspot_x, hotspot_y);
  194. }
  195. static void
  196. setCursorImage(_GLFWwindow* window, bool on_theme_change) {
  197. _GLFWcursorWayland defaultCursor = {.shape = GLFW_DEFAULT_CURSOR};
  198. _GLFWcursorWayland* cursorWayland = window->cursor ? &window->cursor->wl : &defaultCursor;
  199. if (_glfw.wl.wp_cursor_shape_device_v1) {
  200. wayland_cursor_shape s = glfw_cursor_shape_to_wayland_cursor_shape(cursorWayland->shape);
  201. if (s.which > -1) {
  202. debug("Changing cursor shape to: %s with serial: %u\n", s.name, _glfw.wl.pointer_enter_serial);
  203. wp_cursor_shape_device_v1_set_shape(_glfw.wl.wp_cursor_shape_device_v1, _glfw.wl.pointer_enter_serial, (uint32_t)s.which);
  204. return;
  205. }
  206. }
  207. struct wl_cursor_image* image = NULL;
  208. struct wl_buffer* buffer = NULL;
  209. struct wl_surface* surface = _glfw.wl.cursorSurface;
  210. const int scale = _glfwWaylandIntegerWindowScale(window);
  211. if (!_glfw.wl.pointer) return;
  212. if (cursorWayland->scale < 0) {
  213. buffer = cursorWayland->buffer;
  214. toggleTimer(&_glfw.wl.eventLoopData, _glfw.wl.cursorAnimationTimer, 0);
  215. } else {
  216. if (on_theme_change || cursorWayland->scale != scale) {
  217. struct wl_cursor *newCursor = NULL;
  218. struct wl_cursor_theme *theme = glfw_wlc_theme_for_scale(scale);
  219. if (theme) newCursor = _glfwLoadCursor(cursorWayland->shape, theme);
  220. if (newCursor != NULL) {
  221. cursorWayland->cursor = newCursor;
  222. cursorWayland->scale = scale;
  223. cursorWayland->currentImage = 0;
  224. } else {
  225. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: late cursor load failed; proceeding with existing cursor");
  226. }
  227. }
  228. if (!cursorWayland->cursor || !cursorWayland->cursor->image_count || !cursorWayland->cursor->images) return;
  229. if (cursorWayland->currentImage >= cursorWayland->cursor->image_count) cursorWayland->currentImage = 0;
  230. image = cursorWayland->cursor->images[cursorWayland->currentImage];
  231. if (!image) image = cursorWayland->cursor->images[0];
  232. if (!image) return;
  233. buffer = wl_cursor_image_get_buffer(image);
  234. if (image->delay && window->cursor) {
  235. changeTimerInterval(&_glfw.wl.eventLoopData, _glfw.wl.cursorAnimationTimer, ms_to_monotonic_t(image->delay));
  236. toggleTimer(&_glfw.wl.eventLoopData, _glfw.wl.cursorAnimationTimer, 1);
  237. } else {
  238. toggleTimer(&_glfw.wl.eventLoopData, _glfw.wl.cursorAnimationTimer, 0);
  239. }
  240. if (!buffer)
  241. return;
  242. cursorWayland->width = image->width;
  243. cursorWayland->height = image->height;
  244. cursorWayland->xhot = image->hotspot_x;
  245. cursorWayland->yhot = image->hotspot_y;
  246. }
  247. set_cursor_surface(surface, cursorWayland->xhot / scale, cursorWayland->yhot / scale, "setCursorImage");
  248. wl_surface_set_buffer_scale(surface, scale);
  249. wl_surface_attach(surface, buffer, 0, 0);
  250. wl_surface_damage(surface, 0, 0,
  251. cursorWayland->width, cursorWayland->height);
  252. wl_surface_commit(surface);
  253. }
  254. static bool
  255. checkScaleChange(_GLFWwindow* window) {
  256. if (window->wl.expect_scale_from_compositor) return false;
  257. unsigned int scale = 1, monitorScale;
  258. int i;
  259. // Check if we will be able to set the buffer scale or not.
  260. if (_glfw.wl.compositorVersion < 3)
  261. return false;
  262. // Get the scale factor from the highest scale monitor that this window is on
  263. for (i = 0; i < window->wl.monitorsCount; ++i)
  264. {
  265. monitorScale = window->wl.monitors[i]->wl.scale;
  266. if (scale < monitorScale)
  267. scale = monitorScale;
  268. }
  269. if (window->wl.monitorsCount < 1 && _glfw.monitorCount > 0) {
  270. // The window has not yet been assigned to any monitors, use the primary monitor
  271. _GLFWmonitor *m = _glfw.monitors[0];
  272. if (m && m->wl.scale > (int)scale) scale = m->wl.scale;
  273. }
  274. // Only change the framebuffer size if the scale changed.
  275. if (scale != window->wl.integer_scale.deduced && !window->wl.fractional_scale)
  276. {
  277. window->wl.integer_scale.deduced = scale;
  278. setCursorImage(window, false);
  279. return true;
  280. }
  281. if (window->wl.monitorsCount > 0 && !window->wl.initial_scale_notified) {
  282. window->wl.initial_scale_notified = true;
  283. return true;
  284. }
  285. return false;
  286. }
  287. static void
  288. update_regions(_GLFWwindow* window) {
  289. if (!window->wl.transparent) {
  290. struct wl_region* region = wl_compositor_create_region(_glfw.wl.compositor);
  291. if (!region) return;
  292. wl_region_add(region, 0, 0, window->wl.width, window->wl.height);
  293. // Makes the surface considered as XRGB instead of ARGB.
  294. wl_surface_set_opaque_region(window->wl.surface, region);
  295. wl_region_destroy(region);
  296. }
  297. // Set blur region
  298. if (_glfw.wl.org_kde_kwin_blur_manager) {
  299. if (window->wl.has_blur) {
  300. if (!window->wl.org_kde_kwin_blur)
  301. window->wl.org_kde_kwin_blur = org_kde_kwin_blur_manager_create(_glfw.wl.org_kde_kwin_blur_manager, window->wl.surface);
  302. if (window->wl.org_kde_kwin_blur) {
  303. // NULL means entire window
  304. org_kde_kwin_blur_set_region(window->wl.org_kde_kwin_blur, NULL);
  305. org_kde_kwin_blur_commit(window->wl.org_kde_kwin_blur);
  306. }
  307. } else {
  308. org_kde_kwin_blur_manager_unset(_glfw.wl.org_kde_kwin_blur_manager, window->wl.surface);
  309. if (window->wl.org_kde_kwin_blur) { org_kde_kwin_blur_release(window->wl.org_kde_kwin_blur); window->wl.org_kde_kwin_blur = NULL; }
  310. }
  311. }
  312. }
  313. int
  314. _glfwWaylandIntegerWindowScale(_GLFWwindow *window) {
  315. int ans = (window->wl.integer_scale.preferred) ? window->wl.integer_scale.preferred : window->wl.integer_scale.deduced;
  316. if (ans < 1) ans = 1;
  317. return ans;
  318. }
  319. double
  320. _glfwWaylandWindowScale(_GLFWwindow *window) {
  321. double ans = _glfwWaylandIntegerWindowScale(window);
  322. if (window->wl.fractional_scale) ans = window->wl.fractional_scale / 120.;
  323. return ans;
  324. }
  325. static void
  326. wait_for_swap_to_commit(_GLFWwindow *window) {
  327. window->wl.waiting_for_swap_to_commit = true;
  328. debug("Waiting for swap to commit Wayland surface for window: %llu\n", window->id);
  329. }
  330. static void
  331. resizeFramebuffer(_GLFWwindow* window) {
  332. GLFWwindow *ctx = glfwGetCurrentContext();
  333. bool ctx_changed = false;
  334. if (ctx != (GLFWwindow*)window && window->context.client != GLFW_NO_API) { ctx_changed = true; glfwMakeContextCurrent((GLFWwindow*)window); }
  335. double scale = _glfwWaylandWindowScale(window);
  336. int scaled_width = (int)round(window->wl.width * scale);
  337. int scaled_height = (int)round(window->wl.height * scale);
  338. debug("Resizing framebuffer of window: %llu to: %dx%d window size: %dx%d at scale: %.3f\n",
  339. window->id, scaled_width, scaled_height, window->wl.width, window->wl.height, scale);
  340. wl_egl_window_resize(window->wl.native, scaled_width, scaled_height, 0, 0);
  341. update_regions(window);
  342. wait_for_swap_to_commit(window);
  343. if (ctx_changed) glfwMakeContextCurrent(ctx);
  344. _glfwInputFramebufferSize(window, scaled_width, scaled_height);
  345. }
  346. void
  347. _glfwWaylandAfterBufferSwap(_GLFWwindow* window) {
  348. if (window->wl.temp_buffer_used_during_window_creation) {
  349. wl_buffer_destroy(window->wl.temp_buffer_used_during_window_creation);
  350. window->wl.temp_buffer_used_during_window_creation = NULL;
  351. }
  352. if (window->wl.waiting_for_swap_to_commit) {
  353. debug("Window %llu swapped committing surface\n", window->id);
  354. window->wl.waiting_for_swap_to_commit = false;
  355. // this is not really needed, since I think eglSwapBuffers() calls wl_surface_commit()
  356. // but lets be safe. See https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/egl/drivers/dri2/platform_wayland.c#L1510
  357. commit_window_surface(window);
  358. }
  359. }
  360. static const char*
  361. clipboard_mime(void) {
  362. static char buf[128] = {0};
  363. if (buf[0] == 0) {
  364. snprintf(buf, sizeof(buf), "application/glfw+clipboard-%d", getpid());
  365. }
  366. return buf;
  367. }
  368. static void
  369. apply_scale_changes(_GLFWwindow *window, bool resize_framebuffer, bool update_csd) {
  370. double scale = _glfwWaylandWindowScale(window);
  371. if (resize_framebuffer) resizeFramebuffer(window);
  372. _glfwInputWindowContentScale(window, (float)scale, (float)scale);
  373. if (update_csd) csd_set_visible(window, true); // resize the csd iff the window currently has CSD
  374. int buffer_scale = window->wl.fractional_scale ? 1 : (int)scale;
  375. wl_surface_set_buffer_scale(window->wl.surface, buffer_scale);
  376. }
  377. static bool
  378. dispatchChangesAfterConfigure(_GLFWwindow *window, int32_t width, int32_t height) {
  379. bool size_changed = width != window->wl.width || height != window->wl.height;
  380. bool scale_changed = checkScaleChange(window);
  381. if (size_changed) {
  382. _glfwInputWindowSize(window, width, height);
  383. window->wl.width = width; window->wl.height = height;
  384. resizeFramebuffer(window);
  385. }
  386. if (scale_changed) {
  387. debug("Scale changed to %.3f in dispatchChangesAfterConfigure for window: %llu\n", _glfwWaylandWindowScale(window), window->id);
  388. apply_scale_changes(window, !size_changed, false);
  389. }
  390. _glfwInputWindowDamage(window);
  391. return size_changed || scale_changed;
  392. }
  393. static void
  394. inform_compositor_of_window_geometry(_GLFWwindow *window, const char *event) {
  395. #define geometry window->wl.decorations.geometry
  396. debug("Setting window %llu \"visible area\" geometry in %s event: x=%d y=%d %dx%d viewport: %dx%d\n",
  397. window->id, event, geometry.x, geometry.y, geometry.width, geometry.height, window->wl.width, window->wl.height);
  398. xdg_surface_set_window_geometry(window->wl.xdg.surface, geometry.x, geometry.y, geometry.width, geometry.height);
  399. if (window->wl.wp_viewport) wp_viewport_set_destination(window->wl.wp_viewport, window->wl.width, window->wl.height);
  400. #undef geometry
  401. }
  402. static void
  403. xdgDecorationHandleConfigure(void* data,
  404. struct zxdg_toplevel_decoration_v1* decoration UNUSED,
  405. uint32_t mode)
  406. {
  407. _GLFWwindow* window = data;
  408. window->wl.pending.decoration_mode = mode;
  409. window->wl.pending_state |= PENDING_STATE_DECORATION;
  410. debug("XDG decoration configure event received for window %llu: has_server_side_decorations: %d\n", window->id, (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE));
  411. }
  412. static const struct zxdg_toplevel_decoration_v1_listener xdgDecorationListener = {
  413. xdgDecorationHandleConfigure,
  414. };
  415. static void surfaceHandleEnter(void *data,
  416. struct wl_surface *surface UNUSED,
  417. struct wl_output *output)
  418. {
  419. _GLFWwindow* window = data;
  420. _GLFWmonitor* monitor = wl_output_get_user_data(output);
  421. if (window->wl.monitorsCount + 1 > window->wl.monitorsSize)
  422. {
  423. ++window->wl.monitorsSize;
  424. window->wl.monitors =
  425. realloc(window->wl.monitors,
  426. window->wl.monitorsSize * sizeof(_GLFWmonitor*));
  427. }
  428. window->wl.monitors[window->wl.monitorsCount++] = monitor;
  429. if (checkScaleChange(window)) {
  430. debug("Scale changed to %.3f for window %llu in surfaceHandleEnter\n", _glfwWaylandWindowScale(window), window->id);
  431. apply_scale_changes(window, true, true);
  432. }
  433. }
  434. static void surfaceHandleLeave(void *data,
  435. struct wl_surface *surface UNUSED,
  436. struct wl_output *output)
  437. {
  438. _GLFWwindow* window = data;
  439. _GLFWmonitor* monitor = wl_output_get_user_data(output);
  440. bool found;
  441. int i;
  442. for (i = 0, found = false; i < window->wl.monitorsCount - 1; ++i)
  443. {
  444. if (monitor == window->wl.monitors[i])
  445. found = true;
  446. if (found)
  447. window->wl.monitors[i] = window->wl.monitors[i + 1];
  448. }
  449. window->wl.monitors[--window->wl.monitorsCount] = NULL;
  450. if (checkScaleChange(window)) {
  451. debug("Scale changed to %.3f for window %llu in surfaceHandleLeave\n", _glfwWaylandWindowScale(window), window->id);
  452. apply_scale_changes(window, true, true);
  453. }
  454. }
  455. #ifdef WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION
  456. static void
  457. surface_preferred_buffer_scale(void *data, struct wl_surface *surface UNUSED, int32_t scale) {
  458. _GLFWwindow* window = data;
  459. window->wl.once.preferred_scale_received = true;
  460. if ((int)window->wl.integer_scale.preferred == scale && window->wl.window_fully_created) return;
  461. debug("Preferred integer buffer scale changed to: %d for window %llu\n", scale, window->id);
  462. window->wl.integer_scale.preferred = scale;
  463. window->wl.window_fully_created = window->wl.once.surface_configured;
  464. if (!window->wl.fractional_scale) apply_scale_changes(window, true, true);
  465. }
  466. static void
  467. surface_preferred_buffer_transform(void *data, struct wl_surface *surface, uint32_t transform) {
  468. (void)data; (void)surface; (void)transform;
  469. }
  470. #endif
  471. static const struct wl_surface_listener surfaceListener = {
  472. .enter = surfaceHandleEnter,
  473. .leave = surfaceHandleLeave,
  474. #ifdef WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION
  475. .preferred_buffer_scale = &surface_preferred_buffer_scale,
  476. .preferred_buffer_transform = &surface_preferred_buffer_transform,
  477. #endif
  478. };
  479. static void
  480. fractional_scale_preferred_scale(void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1 UNUSED, uint32_t scale) {
  481. _GLFWwindow *window = data;
  482. window->wl.once.fractional_scale_received = true;
  483. if (scale == window->wl.fractional_scale && window->wl.window_fully_created) return;
  484. debug("Fractional scale requested: %u/120 = %.2f for window %llu\n", scale, scale / 120., window->id);
  485. window->wl.fractional_scale = scale;
  486. // niri and up-to-date mutter and up-to-date kwin all send the fractional
  487. // scale before configure (as of Jan 2025). sway as of 1.10 and Hyprland send it after configure.
  488. // https://github.com/hyprwm/Hyprland/issues/9126
  489. // labwc doesnt support preferred buffer scale and seems to send only a
  490. // single fraction scale event before configure https://github.com/kovidgoyal/kitty/issues/7540
  491. window->wl.window_fully_created = window->wl.once.surface_configured;
  492. apply_scale_changes(window, true, true);
  493. }
  494. static const struct wp_fractional_scale_v1_listener fractional_scale_listener = {
  495. .preferred_scale = &fractional_scale_preferred_scale,
  496. };
  497. static bool createSurface(_GLFWwindow* window,
  498. const _GLFWwndconfig* wndconfig)
  499. {
  500. window->wl.surface = wl_compositor_create_surface(_glfw.wl.compositor);
  501. if (!window->wl.surface)
  502. return false;
  503. wl_surface_add_listener(window->wl.surface,
  504. &surfaceListener,
  505. window);
  506. wl_surface_set_user_data(window->wl.surface, window);
  507. // If we already have been notified of the primary monitor scale, assume
  508. // the window will be created on it and so avoid a rescale roundtrip in the common
  509. // case of the window being shown on the primary monitor or all monitors having the same scale.
  510. // If you change this also change get_window_content_scale() in the kitty code.
  511. GLFWmonitor* monitor = glfwGetPrimaryMonitor();
  512. float xscale = 1.0, yscale = 1.0;
  513. int scale = 1;
  514. if (monitor) {
  515. glfwGetMonitorContentScale(monitor, &xscale, &yscale);
  516. // see wl_monitor.c xscale is always == yscale
  517. if (xscale <= 0.0001 || xscale != xscale || xscale >= 24) xscale = 1.0;
  518. if (xscale > 1) scale = (int)xscale;
  519. }
  520. window->wl.expect_scale_from_compositor = _glfw.wl.has_preferred_buffer_scale;
  521. if (_glfw.wl.wp_fractional_scale_manager_v1 && _glfw.wl.wp_viewporter) {
  522. window->wl.wp_fractional_scale_v1 = wp_fractional_scale_manager_v1_get_fractional_scale(_glfw.wl.wp_fractional_scale_manager_v1, window->wl.surface);
  523. if (window->wl.wp_fractional_scale_v1) {
  524. window->wl.wp_viewport = wp_viewporter_get_viewport(_glfw.wl.wp_viewporter, window->wl.surface);
  525. if (window->wl.wp_viewport) {
  526. wp_fractional_scale_v1_add_listener(window->wl.wp_fractional_scale_v1, &fractional_scale_listener, window);
  527. window->wl.expect_scale_from_compositor = true;
  528. }
  529. }
  530. }
  531. window->wl.window_fully_created = !window->wl.expect_scale_from_compositor;
  532. if (_glfw.wl.org_kde_kwin_blur_manager && wndconfig->blur_radius > 0) _glfwPlatformSetWindowBlur(window, wndconfig->blur_radius);
  533. window->wl.integer_scale.deduced = scale;
  534. if (_glfw.wl.has_preferred_buffer_scale) { scale = 1; window->wl.integer_scale.preferred = 1; }
  535. debug("Creating window %llu at size: %dx%d and scale %d\n", window->id, wndconfig->width, wndconfig->height, scale);
  536. window->wl.native = wl_egl_window_create(window->wl.surface, wndconfig->width * scale, wndconfig->height * scale);
  537. if (!window->wl.native)
  538. return false;
  539. window->wl.width = wndconfig->width;
  540. window->wl.height = wndconfig->height;
  541. window->wl.user_requested_content_size.width = wndconfig->width;
  542. window->wl.user_requested_content_size.height = wndconfig->height;
  543. update_regions(window);
  544. wl_surface_set_buffer_scale(window->wl.surface, scale);
  545. return true;
  546. }
  547. static void
  548. setFullscreen(_GLFWwindow* window, _GLFWmonitor* monitor, bool on) {
  549. if (!window->wl.xdg.toplevel) return;
  550. if (!window->wl.wm_capabilities.fullscreen) {
  551. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland compositor does not support fullscreen");
  552. return;
  553. }
  554. if (on) xdg_toplevel_set_fullscreen(window->wl.xdg.toplevel, monitor ? monitor->wl.output : NULL);
  555. else xdg_toplevel_unset_fullscreen(window->wl.xdg.toplevel);
  556. }
  557. bool
  558. _glfwPlatformIsFullscreen(_GLFWwindow *window, unsigned int flags UNUSED) {
  559. return window->wl.current.toplevel_states & TOPLEVEL_STATE_FULLSCREEN;
  560. }
  561. bool
  562. _glfwPlatformToggleFullscreen(_GLFWwindow *window, unsigned int flags UNUSED) {
  563. bool already_fullscreen = _glfwPlatformIsFullscreen(window, flags);
  564. setFullscreen(window, NULL, !already_fullscreen);
  565. return !already_fullscreen;
  566. }
  567. static void
  568. report_live_resize(_GLFWwindow *w, bool started) {
  569. // disabled as mutter, for instance, does not send a configure event when the user stops resizing (aka releases the mouse button)
  570. if (false) _glfwInputLiveResize(w, started);
  571. }
  572. static void
  573. xdgToplevelHandleConfigure(void* data,
  574. struct xdg_toplevel* toplevel UNUSED,
  575. int32_t width,
  576. int32_t height,
  577. struct wl_array* states)
  578. {
  579. _GLFWwindow* window = data;
  580. float aspectRatio;
  581. float targetRatio;
  582. enum xdg_toplevel_state* state;
  583. uint32_t new_states = 0;
  584. debug("XDG top-level configure event for window %llu: size: %dx%d states: ", window->id, width, height);
  585. wl_array_for_each(state, states) {
  586. switch (*state) {
  587. #define C(x) case XDG_##x: new_states |= x; debug("%s ", #x); break
  588. C(TOPLEVEL_STATE_RESIZING);
  589. C(TOPLEVEL_STATE_MAXIMIZED);
  590. C(TOPLEVEL_STATE_FULLSCREEN);
  591. C(TOPLEVEL_STATE_ACTIVATED);
  592. C(TOPLEVEL_STATE_TILED_LEFT);
  593. C(TOPLEVEL_STATE_TILED_RIGHT);
  594. C(TOPLEVEL_STATE_TILED_TOP);
  595. C(TOPLEVEL_STATE_TILED_BOTTOM);
  596. #ifdef XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION
  597. C(TOPLEVEL_STATE_SUSPENDED);
  598. #endif
  599. #ifdef XDG_TOPLEVEL_STATE_CONSTRAINED_LEFT_SINCE_VERSION
  600. C(TOPLEVEL_STATE_CONSTRAINED_LEFT);
  601. C(TOPLEVEL_STATE_CONSTRAINED_RIGHT);
  602. C(TOPLEVEL_STATE_CONSTRAINED_TOP);
  603. C(TOPLEVEL_STATE_CONSTRAINED_BOTTOM);
  604. #endif
  605. #undef C
  606. }
  607. }
  608. debug("\n");
  609. if (new_states & TOPLEVEL_STATE_RESIZING) {
  610. if (width) window->wl.user_requested_content_size.width = width;
  611. if (height) window->wl.user_requested_content_size.height = height;
  612. if (!(window->wl.current.toplevel_states & TOPLEVEL_STATE_RESIZING)) report_live_resize(window, true);
  613. }
  614. if (width != 0 && height != 0)
  615. {
  616. if (!(new_states & TOPLEVEL_STATE_DOCKED))
  617. {
  618. if (window->numer != GLFW_DONT_CARE && window->denom != GLFW_DONT_CARE)
  619. {
  620. aspectRatio = (float)width / (float)height;
  621. targetRatio = (float)window->numer / (float)window->denom;
  622. if (aspectRatio < targetRatio)
  623. height = (int32_t)((float)width / targetRatio);
  624. else if (aspectRatio > targetRatio)
  625. width = (int32_t)((float)height * targetRatio);
  626. }
  627. }
  628. }
  629. window->wl.pending.toplevel_states = new_states;
  630. window->wl.pending.width = width;
  631. window->wl.pending.height = height;
  632. window->wl.pending_state |= PENDING_STATE_TOPLEVEL;
  633. }
  634. static void xdgToplevelHandleClose(void* data,
  635. struct xdg_toplevel* toplevel UNUSED)
  636. {
  637. _GLFWwindow* window = data;
  638. window->wl.window_fully_created = true;
  639. _glfwInputWindowCloseRequest(window);
  640. }
  641. #if defined(XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION)
  642. static void
  643. xdg_toplevel_wm_capabilities(void *data, struct xdg_toplevel *xdg_toplevel UNUSED, struct wl_array *caps) {
  644. _GLFWwindow *window = data;
  645. #define c (window->wl.wm_capabilities)
  646. memset(&c, 0, sizeof(c));
  647. enum xdg_toplevel_wm_capabilities *cap;
  648. wl_array_for_each(cap, caps) {
  649. switch (*cap) {
  650. case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE: c.maximize = true; break;
  651. case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE: c.minimize = true; break;
  652. case XDG_TOPLEVEL_WM_CAPABILITIES_WINDOW_MENU: c.window_menu = true; break;
  653. case XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN: c.fullscreen = true; break;
  654. }
  655. }
  656. debug("Compositor top-level capabilities: maximize=%d minimize=%d window_menu=%d fullscreen=%d\n",
  657. c.maximize, c.minimize, c.window_menu, c.fullscreen);
  658. #undef c
  659. }
  660. #endif
  661. static void
  662. xdg_toplevel_configure_bounds(void *data, struct xdg_toplevel *xdg_toplevel UNUSED, int32_t width, int32_t height) {
  663. _GLFWwindow *window = data;
  664. window->wl.xdg.top_level_bounds.width = width;
  665. window->wl.xdg.top_level_bounds.height = height;
  666. debug("Compositor set top-level bounds of: %dx%d for window %llu\n", width, height, window->id);
  667. }
  668. static const struct xdg_toplevel_listener xdgToplevelListener = {
  669. .configure = xdgToplevelHandleConfigure,
  670. .close = xdgToplevelHandleClose,
  671. #ifdef XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION
  672. .configure_bounds = xdg_toplevel_configure_bounds,
  673. .wm_capabilities = xdg_toplevel_wm_capabilities,
  674. #endif
  675. };
  676. static void
  677. update_fully_created_on_configure(_GLFWwindow *window) {
  678. // See fractional_scale_preferred_scale() for logic
  679. if (!window->wl.window_fully_created) {
  680. window->wl.window_fully_created = window->wl.once.fractional_scale_received;
  681. if (window->wl.window_fully_created) debug("Marked window as fully created in configure event\n");
  682. }
  683. }
  684. static void
  685. apply_xdg_configure_changes(_GLFWwindow *window) {
  686. bool suspended_changed = false;
  687. if (window->wl.pending_state & PENDING_STATE_TOPLEVEL) {
  688. uint32_t new_states = window->wl.pending.toplevel_states;
  689. int width = window->wl.pending.width;
  690. int height = window->wl.pending.height;
  691. if (!window->wl.once.surface_configured) {
  692. window->swaps_disallowed = false;
  693. wait_for_swap_to_commit(window);
  694. window->wl.once.surface_configured = true;
  695. update_fully_created_on_configure(window);
  696. }
  697. #ifdef XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION
  698. suspended_changed = ((new_states & TOPLEVEL_STATE_SUSPENDED) != (window->wl.current.toplevel_states & TOPLEVEL_STATE_SUSPENDED));
  699. #endif
  700. if (new_states != window->wl.current.toplevel_states ||
  701. width != window->wl.current.width ||
  702. height != window->wl.current.height) {
  703. bool live_resize_done = !(new_states & TOPLEVEL_STATE_RESIZING) && (window->wl.current.toplevel_states & TOPLEVEL_STATE_RESIZING);
  704. window->wl.current.toplevel_states = new_states;
  705. window->wl.current.width = width;
  706. window->wl.current.height = height;
  707. _glfwInputWindowFocus(window, window->wl.current.toplevel_states & TOPLEVEL_STATE_ACTIVATED);
  708. if (live_resize_done) report_live_resize(window, false);
  709. }
  710. }
  711. if (window->wl.pending_state & PENDING_STATE_DECORATION) {
  712. uint32_t mode = window->wl.pending.decoration_mode;
  713. bool has_server_side_decorations = (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
  714. window->wl.decorations.serverSide = has_server_side_decorations;
  715. window->wl.current.decoration_mode = mode;
  716. }
  717. if (window->wl.pending_state) {
  718. int width = window->wl.pending.width, height = window->wl.pending.height;
  719. csd_set_window_geometry(window, &width, &height);
  720. bool resized = dispatchChangesAfterConfigure(window, width, height);
  721. csd_set_visible(window, !(window->wl.decorations.serverSide || window->monitor || window->wl.current.toplevel_states & TOPLEVEL_STATE_FULLSCREEN));
  722. debug("Final window %llu content size: %dx%d resized: %d\n", window->id, width, height, resized);
  723. }
  724. inform_compositor_of_window_geometry(window, "configure");
  725. commit_window_surface_if_safe(window);
  726. window->wl.pending_state = 0;
  727. #ifdef XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION
  728. if (suspended_changed) {
  729. _glfwInputWindowOcclusion(window, window->wl.current.toplevel_states & TOPLEVEL_STATE_SUSPENDED);
  730. }
  731. #endif
  732. }
  733. typedef union pixel {
  734. struct {
  735. uint8_t blue, green, red, alpha;
  736. };
  737. uint32_t value;
  738. } pixel;
  739. static struct wl_buffer*
  740. create_single_color_buffer(int width, int height, pixel color) {
  741. // convert to pre-multiplied alpha as that's what wayland wants
  742. if (width == 1 && height == 1 && _glfw.wl.wp_single_pixel_buffer_manager_v1) {
  743. #define C(x) (uint32_t)(((double)((uint64_t)color.alpha * color.x * UINT32_MAX)) / (255 * 255))
  744. struct wl_buffer *ans = wp_single_pixel_buffer_manager_v1_create_u32_rgba_buffer(
  745. _glfw.wl.wp_single_pixel_buffer_manager_v1, C(red), C(green), C(blue), (uint32_t)((color.alpha / 255.) * UINT32_MAX));
  746. #undef C
  747. if (!ans) _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to create single pixel buffer");
  748. return ans;
  749. }
  750. float alpha = color.alpha / 255.f;
  751. color.red = (uint8_t)(alpha * color.red); color.green = (uint8_t)(alpha * color.green); color.blue = (uint8_t)(alpha * color.blue);
  752. int shm_format = color.alpha == 0xff ? WL_SHM_FORMAT_XRGB8888 : WL_SHM_FORMAT_ARGB8888;
  753. const size_t size = 4 * width * height;
  754. int fd = createAnonymousFile(size);
  755. if (fd < 0) {
  756. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to create anonymous file");
  757. return NULL;
  758. }
  759. uint32_t *shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  760. if (color.value) for (size_t i = 0; i < size/4; i++) shm_data[i] = color.value;
  761. else memset(shm_data, 0, size);
  762. if (!shm_data) {
  763. close(fd);
  764. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to mmap anonymous file");
  765. return NULL;
  766. }
  767. struct wl_shm_pool *pool = wl_shm_create_pool(_glfw.wl.shm, fd, size);
  768. if (!pool) {
  769. close(fd); munmap(shm_data, size);
  770. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to create wl_shm_pool of size: %zu", size);
  771. return NULL;
  772. }
  773. struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, width, height, width * 4, shm_format);
  774. wl_shm_pool_destroy(pool); munmap(shm_data, size); close(fd);
  775. if (!buffer) {
  776. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to create wl_buffer of size: %zu", size);
  777. return NULL;
  778. }
  779. return buffer;
  780. }
  781. static bool
  782. attach_temp_buffer_during_window_creation(_GLFWwindow *window) {
  783. pixel color;
  784. color.value = _glfw.hints.window.wl.bgcolor;
  785. if (!window->wl.transparent) color.alpha = 0xff;
  786. else if (color.alpha == 0) color.value = 0; // fully transparent blends best with black and we can use memset
  787. if (window->wl.temp_buffer_used_during_window_creation) {
  788. wl_buffer_destroy(window->wl.temp_buffer_used_during_window_creation);
  789. window->wl.temp_buffer_used_during_window_creation = NULL;
  790. }
  791. int width, height;
  792. _glfwPlatformGetFramebufferSize(window, &width, &height);
  793. if (window->wl.wp_viewport) {
  794. window->wl.temp_buffer_used_during_window_creation = create_single_color_buffer(1, 1, color);
  795. wl_surface_set_buffer_scale(window->wl.surface, 1);
  796. wp_viewport_set_destination(window->wl.wp_viewport, window->wl.width, window->wl.height);
  797. } else {
  798. window->wl.temp_buffer_used_during_window_creation = create_single_color_buffer(width, height, color);
  799. wl_surface_set_buffer_scale(window->wl.surface, window->wl.fractional_scale ? 1: _glfwWaylandIntegerWindowScale(window));
  800. }
  801. if (!window->wl.temp_buffer_used_during_window_creation) return false;
  802. wl_surface_attach(window->wl.surface, window->wl.temp_buffer_used_during_window_creation, 0, 0);
  803. debug("Attached temp buffer during window %llu creation of size: %dx%d and rgba(%u, %u, %u, %u)\n", window->id, width, height, color.red, color.green, color.blue, color.alpha);
  804. commit_window_surface(window);
  805. return true;
  806. }
  807. static void
  808. loop_till_window_fully_created(_GLFWwindow *window) {
  809. if (!window->wl.window_fully_created) {
  810. GLFWwindow *ctx = glfwGetCurrentContext();
  811. debug("Waiting for compositor to send fractional scale for window %llu\n", window->id);
  812. monotonic_t start = monotonic();
  813. while (!window->wl.window_fully_created && monotonic() - start < ms_to_monotonic_t(300)) {
  814. if (wl_display_roundtrip(_glfw.wl.display) == -1) {
  815. window->wl.window_fully_created = true;
  816. }
  817. }
  818. window->wl.window_fully_created = true;
  819. // If other OS windows were resized when this window is shown, the ctx might have been changed by
  820. // user code, restore it to whatever it was at the start.
  821. if (glfwGetCurrentContext() != ctx) glfwMakeContextCurrent(ctx);
  822. }
  823. }
  824. static void
  825. xdgSurfaceHandleConfigure(void* data, struct xdg_surface* surface, uint32_t serial) {
  826. // The poorly documented pattern Wayland requires is:
  827. // 1) ack the configure,
  828. // 2) set the window geometry
  829. // 3) attach a new buffer of the correct size to the surface
  830. // 4) only then commit the surface.
  831. // buffer is attached only by eglSwapBuffers,
  832. // so we set a flag to not commit the surface till the next swapbuffers. Note that
  833. // wl_egl_window_resize() does not actually resize the buffer until the next draw call
  834. // or buffer state query.
  835. _GLFWwindow* window = data;
  836. xdg_surface_ack_configure(surface, serial);
  837. debug("XDG surface configure event received and acknowledged for window %llu\n", window->id);
  838. apply_xdg_configure_changes(window);
  839. if (!window->wl.window_fully_created) {
  840. if (!attach_temp_buffer_during_window_creation(window)) window->wl.window_fully_created = true;
  841. }
  842. }
  843. static const struct xdg_surface_listener xdgSurfaceListener = {
  844. xdgSurfaceHandleConfigure
  845. };
  846. static void
  847. setXdgDecorations(_GLFWwindow* window)
  848. {
  849. if (window->wl.xdg.decoration) {
  850. window->wl.decorations.serverSide = true;
  851. zxdg_toplevel_decoration_v1_set_mode(window->wl.xdg.decoration, window->decorated ? ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE: ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE);
  852. } else {
  853. window->wl.decorations.serverSide = false;
  854. csd_set_visible(window, window->decorated);
  855. }
  856. }
  857. void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, bool enabled UNUSED) {
  858. setXdgDecorations(window);
  859. inform_compositor_of_window_geometry(window, "SetWindowDecorated");
  860. commit_window_surface_if_safe(window);
  861. }
  862. static struct wl_output*
  863. find_output_by_name(const char* name) {
  864. if (!name || !name[0]) return NULL;
  865. for (int i = 0; i < _glfw.monitorCount; i++) {
  866. _GLFWmonitor *m = _glfw.monitors[i];
  867. if (strcmp(m->name, name) == 0) return m->wl.output;
  868. }
  869. return NULL;
  870. }
  871. static enum zwlr_layer_shell_v1_layer
  872. get_layer_shell_layer(const _GLFWwindow *window) {
  873. enum zwlr_layer_shell_v1_layer which_layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND; // Default to background
  874. switch (window->wl.layer_shell.config.type) {
  875. case GLFW_LAYER_SHELL_BACKGROUND: case GLFW_LAYER_SHELL_NONE: break;
  876. case GLFW_LAYER_SHELL_PANEL: which_layer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM; break;
  877. case GLFW_LAYER_SHELL_TOP: which_layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP; break;
  878. case GLFW_LAYER_SHELL_OVERLAY: which_layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY; break;
  879. }
  880. return which_layer;
  881. }
  882. static void
  883. layer_set_properties(const _GLFWwindow *window, bool during_creation, uint32_t width, uint32_t height) {
  884. #define config window->wl.layer_shell.config
  885. enum zwlr_layer_surface_v1_anchor which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
  886. int exclusive_zone = config.requested_exclusive_zone;
  887. enum zwlr_layer_surface_v1_keyboard_interactivity focus_policy = ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE;
  888. switch(config.focus_policy) {
  889. case GLFW_FOCUS_NOT_ALLOWED: focus_policy = ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE; break;
  890. case GLFW_FOCUS_EXCLUSIVE: focus_policy = ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE; break;
  891. case GLFW_FOCUS_ON_DEMAND: focus_policy = ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_ON_DEMAND; break;
  892. }
  893. int panel_width = 0, panel_height = 0;
  894. switch (config.type) {
  895. case GLFW_LAYER_SHELL_NONE: break;
  896. case GLFW_LAYER_SHELL_BACKGROUND: exclusive_zone = -1; break;
  897. case GLFW_LAYER_SHELL_TOP:
  898. case GLFW_LAYER_SHELL_OVERLAY:
  899. case GLFW_LAYER_SHELL_PANEL:
  900. switch (config.edge) {
  901. case GLFW_EDGE_TOP:
  902. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
  903. panel_height = height;
  904. if (!config.override_exclusive_zone) exclusive_zone = height;
  905. break;
  906. case GLFW_EDGE_BOTTOM:
  907. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
  908. panel_height = height;
  909. if (!config.override_exclusive_zone) exclusive_zone = height;
  910. break;
  911. case GLFW_EDGE_LEFT:
  912. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
  913. panel_width = width;
  914. if (!config.override_exclusive_zone) exclusive_zone = width;
  915. break;
  916. case GLFW_EDGE_RIGHT:
  917. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
  918. panel_width = width;
  919. if (!config.override_exclusive_zone) exclusive_zone = width;
  920. break;
  921. case GLFW_EDGE_CENTER:
  922. break;
  923. case GLFW_EDGE_CENTER_SIZED:
  924. panel_width = width; panel_height = height;
  925. break;
  926. case GLFW_EDGE_NONE:
  927. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
  928. panel_width = width; panel_height = height;
  929. break;
  930. }
  931. }
  932. #define surface window->wl.layer_shell.zwlr_layer_surface_v1
  933. zwlr_layer_surface_v1_set_size(surface, panel_width, panel_height);
  934. debug("Compositor will be informed that layer size: %dx%d viewport: %dx%d at next surface commit\n", panel_width, panel_height, width, height);
  935. zwlr_layer_surface_v1_set_anchor(surface, which_anchor);
  936. zwlr_layer_surface_v1_set_exclusive_zone(surface, exclusive_zone);
  937. zwlr_layer_surface_v1_set_margin(surface, config.requested_top_margin, config.requested_right_margin, config.requested_bottom_margin, config.requested_left_margin);
  938. if (!during_creation) zwlr_layer_surface_v1_set_layer(surface, get_layer_shell_layer(window));
  939. zwlr_layer_surface_v1_set_keyboard_interactivity(surface, focus_policy);
  940. #undef surface
  941. #undef config
  942. }
  943. static void
  944. calculate_layer_size(_GLFWwindow *window, uint32_t *width, uint32_t *height) {
  945. const GLFWLayerShellConfig *config = &window->wl.layer_shell.config;
  946. GLFWvidmode m = {0};
  947. if (window->wl.monitorsCount) _glfwPlatformGetVideoMode(window->wl.monitors[0], &m);
  948. int monitor_width = m.width, monitor_height = m.height;
  949. const int y_margin = config->requested_bottom_margin + config->requested_top_margin, x_margin = config->requested_left_margin + config->requested_right_margin;
  950. monitor_width = monitor_width > x_margin ? monitor_width - x_margin : 0;
  951. monitor_height = monitor_height > y_margin ? monitor_height - y_margin : 0;
  952. float xscale = (float)config->expected.xscale, yscale = (float)config->expected.yscale;
  953. if (window->wl.window_fully_created) _glfwPlatformGetWindowContentScale(window, &xscale, &yscale);
  954. unsigned cell_width, cell_height; double left_edge_spacing, top_edge_spacing, right_edge_spacing, bottom_edge_spacing;
  955. config->size_callback((GLFWwindow*)window, xscale, yscale, &cell_width, &cell_height, &left_edge_spacing, &top_edge_spacing, &right_edge_spacing, &bottom_edge_spacing);
  956. double spacing_x = left_edge_spacing + right_edge_spacing;
  957. double spacing_y = top_edge_spacing + bottom_edge_spacing;
  958. if (config->type == GLFW_LAYER_SHELL_BACKGROUND) {
  959. if (!*width) *width = monitor_width;
  960. if (!*height) *height = monitor_height;
  961. return;
  962. }
  963. const unsigned xsz = config->x_size_in_pixels ? (unsigned)(config->x_size_in_pixels * xscale) : (cell_width * config->x_size_in_cells);
  964. const unsigned ysz = config->y_size_in_pixels ? (unsigned)(config->y_size_in_pixels * yscale) : (cell_height * config->y_size_in_cells);
  965. debug("Calculating layer shell window size at scale: %f cell_size: %u %u sz: %u %u\n", xscale, cell_width, cell_height, xsz, ysz);
  966. if (config->edge == GLFW_EDGE_LEFT || config->edge == GLFW_EDGE_RIGHT) {
  967. if (!*height) *height = monitor_height;
  968. double spacing = spacing_x;
  969. spacing += xsz / xscale;
  970. *width = (uint32_t)(1. + spacing);
  971. } else if (config->edge == GLFW_EDGE_TOP || config->edge == GLFW_EDGE_BOTTOM) {
  972. if (!*width) *width = monitor_width;
  973. double spacing = spacing_y;
  974. spacing += ysz / yscale;
  975. *height = (uint32_t)(1. + spacing);
  976. } else if (config->edge == GLFW_EDGE_CENTER) {
  977. if (!*width) *width = monitor_width;
  978. if (!*height) *height = monitor_height;
  979. } else {
  980. spacing_x += xsz / xscale;
  981. spacing_y += ysz / yscale;
  982. *width = (uint32_t)(1. + spacing_x);
  983. *height = (uint32_t)(1. + spacing_y);
  984. }
  985. }
  986. static void
  987. layer_surface_handle_configure(void* data, struct zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t width, uint32_t height) {
  988. debug("Layer shell configure event: width: %u height: %u\n", width, height);
  989. _GLFWwindow* window = data;
  990. if (!window->wl.once.surface_configured) {
  991. window->swaps_disallowed = false;
  992. wait_for_swap_to_commit(window);
  993. window->wl.once.surface_configured = true;
  994. update_fully_created_on_configure(window);
  995. }
  996. calculate_layer_size(window, &width, &height);
  997. zwlr_layer_surface_v1_ack_configure(surface, serial);
  998. if ((int)width != window->wl.width || (int)height != window->wl.height) {
  999. debug("Layer shell size changed to %ux%u in layer_surface_handle_configure\n", width, height);
  1000. _glfwInputWindowSize(window, width, height);
  1001. window->wl.width = width; window->wl.height = height;
  1002. resizeFramebuffer(window);
  1003. _glfwInputWindowDamage(window);
  1004. layer_set_properties(window, false, window->wl.width, window->wl.height);
  1005. if (window->wl.wp_viewport) wp_viewport_set_destination(window->wl.wp_viewport, window->wl.width, window->wl.height);
  1006. }
  1007. commit_window_surface_if_safe(window);
  1008. if (!window->wl.window_fully_created) {
  1009. if (!attach_temp_buffer_during_window_creation(window)) window->wl.window_fully_created = true;
  1010. }
  1011. }
  1012. static void
  1013. layer_surface_handle_close_requested(void* data, struct zwlr_layer_surface_v1* surface UNUSED) {
  1014. _GLFWwindow* window = data;
  1015. window->wl.window_fully_created = true;
  1016. _glfwInputWindowCloseRequest(window);
  1017. }
  1018. static const struct zwlr_layer_surface_v1_listener zwlr_layer_surface_v1_listener = {
  1019. .configure=layer_surface_handle_configure,
  1020. .closed=layer_surface_handle_close_requested,
  1021. };
  1022. static bool
  1023. create_layer_shell_surface(_GLFWwindow *window) {
  1024. if (!_glfw.wl.zwlr_layer_shell_v1) {
  1025. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: wlr-layer-shell protocol unsupported by compositor");
  1026. return false;
  1027. }
  1028. window->decorated = false; // shell windows must not have decorations
  1029. struct wl_output *wl_output = find_output_by_name(window->wl.layer_shell.config.output_name);
  1030. #define ls window->wl.layer_shell.zwlr_layer_surface_v1
  1031. ls = zwlr_layer_shell_v1_get_layer_surface(
  1032. _glfw.wl.zwlr_layer_shell_v1, window->wl.surface, wl_output, get_layer_shell_layer(window), window->wl.appId[0] ? window->wl.appId : "kitty");
  1033. if (!ls) {
  1034. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: layer-surface creation failed");
  1035. return false;
  1036. }
  1037. zwlr_layer_surface_v1_add_listener(ls, &zwlr_layer_surface_v1_listener, window);
  1038. layer_set_properties(window, true, window->wl.width, window->wl.height);
  1039. if (window->wl.wp_viewport) wp_viewport_set_destination(window->wl.wp_viewport, window->wl.width, window->wl.height);
  1040. commit_window_surface(window);
  1041. wl_display_roundtrip(_glfw.wl.display);
  1042. window->wl.created = true;
  1043. #undef ls
  1044. return true;
  1045. }
  1046. static bool
  1047. create_window_desktop_surface(_GLFWwindow* window)
  1048. {
  1049. if (is_layer_shell(window)) return create_layer_shell_surface(window);
  1050. window->wl.xdg.surface = xdg_wm_base_get_xdg_surface(_glfw.wl.wmBase,
  1051. window->wl.surface);
  1052. if (!window->wl.xdg.surface)
  1053. {
  1054. _glfwInputError(GLFW_PLATFORM_ERROR,
  1055. "Wayland: xdg-surface creation failed");
  1056. return false;
  1057. }
  1058. xdg_surface_add_listener(window->wl.xdg.surface,
  1059. &xdgSurfaceListener,
  1060. window);
  1061. window->wl.xdg.toplevel = xdg_surface_get_toplevel(window->wl.xdg.surface);
  1062. if (!window->wl.xdg.toplevel)
  1063. {
  1064. _glfwInputError(GLFW_PLATFORM_ERROR,
  1065. "Wayland: xdg-toplevel creation failed");
  1066. return false;
  1067. }
  1068. #ifdef XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION
  1069. if (_glfw.wl.xdg_wm_base_version < XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION) {
  1070. window->wl.wm_capabilities.maximize = true; window->wl.wm_capabilities.minimize = true; window->wl.wm_capabilities.fullscreen = true;
  1071. window->wl.wm_capabilities.window_menu = true;
  1072. }
  1073. #endif
  1074. xdg_toplevel_add_listener(window->wl.xdg.toplevel, &xdgToplevelListener, window);
  1075. if (_glfw.wl.decorationManager) {
  1076. window->wl.xdg.decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(
  1077. _glfw.wl.decorationManager, window->wl.xdg.toplevel);
  1078. zxdg_toplevel_decoration_v1_add_listener(window->wl.xdg.decoration, &xdgDecorationListener, window);
  1079. }
  1080. if (window->wl.appId[0])
  1081. xdg_toplevel_set_app_id(window->wl.xdg.toplevel, window->wl.appId);
  1082. if (window->wl.windowTag[0] && _glfw.wl.xdg_toplevel_tag_manager_v1)
  1083. xdg_toplevel_tag_manager_v1_set_toplevel_tag(_glfw.wl.xdg_toplevel_tag_manager_v1, window->wl.xdg.toplevel, window->wl.windowTag);
  1084. if (window->wl.title)
  1085. xdg_toplevel_set_title(window->wl.xdg.toplevel, window->wl.title);
  1086. if (window->minwidth != GLFW_DONT_CARE && window->minheight != GLFW_DONT_CARE)
  1087. xdg_toplevel_set_min_size(window->wl.xdg.toplevel,
  1088. window->minwidth, window->minheight);
  1089. if (window->maxwidth != GLFW_DONT_CARE && window->maxheight != GLFW_DONT_CARE)
  1090. xdg_toplevel_set_max_size(window->wl.xdg.toplevel,
  1091. window->maxwidth, window->maxheight);
  1092. if (window->monitor) {
  1093. if (window->wl.wm_capabilities.fullscreen)
  1094. xdg_toplevel_set_fullscreen(window->wl.xdg.toplevel, window->monitor->wl.output);
  1095. else
  1096. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland compositor does not support fullscreen");
  1097. } else {
  1098. if (window->wl.maximize_on_first_show) {
  1099. window->wl.maximize_on_first_show = false;
  1100. xdg_toplevel_set_maximized(window->wl.xdg.toplevel);
  1101. }
  1102. setXdgDecorations(window);
  1103. }
  1104. commit_window_surface(window);
  1105. wl_display_roundtrip(_glfw.wl.display);
  1106. window->wl.created = true;
  1107. return true;
  1108. }
  1109. static void incrementCursorImage(_GLFWwindow* window)
  1110. {
  1111. if (window && window->wl.decorations.focus == CENTRAL_WINDOW && window->cursorMode != GLFW_CURSOR_HIDDEN) {
  1112. _GLFWcursor* cursor = window->wl.currentCursor;
  1113. if (cursor && cursor->wl.cursor && cursor->wl.cursor->image_count)
  1114. {
  1115. cursor->wl.currentImage += 1;
  1116. cursor->wl.currentImage %= cursor->wl.cursor->image_count;
  1117. setCursorImage(window, false);
  1118. toggleTimer(&_glfw.wl.eventLoopData, _glfw.wl.cursorAnimationTimer, cursor->wl.cursor->image_count > 1);
  1119. return;
  1120. }
  1121. }
  1122. toggleTimer(&_glfw.wl.eventLoopData, _glfw.wl.cursorAnimationTimer, 1);
  1123. }
  1124. void
  1125. animateCursorImage(id_type timer_id UNUSED, void *data UNUSED) {
  1126. incrementCursorImage(_glfw.wl.pointerFocus);
  1127. }
  1128. static void
  1129. abortOnFatalError(int last_error) {
  1130. static bool abort_called = false;
  1131. if (!abort_called) {
  1132. abort_called = true;
  1133. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: fatal display error: %s", strerror(last_error));
  1134. if (_glfw.callbacks.application_close) _glfw.callbacks.application_close(1);
  1135. else {
  1136. _GLFWwindow* window = _glfw.windowListHead;
  1137. while (window)
  1138. {
  1139. _glfwInputWindowCloseRequest(window);
  1140. window = window->next;
  1141. }
  1142. }
  1143. }
  1144. // ensure the tick callback is called
  1145. _glfw.wl.eventLoopData.wakeup_data_read = true;
  1146. }
  1147. static void
  1148. wayland_read_events(int poll_result, int events, void *data UNUSED) {
  1149. EVDBG("wayland_read_events poll_result: %d events: %d", poll_result, events);
  1150. if (poll_result > 0 && events) wl_display_read_events(_glfw.wl.display);
  1151. else wl_display_cancel_read(_glfw.wl.display);
  1152. }
  1153. static void handleEvents(monotonic_t timeout)
  1154. {
  1155. struct wl_display* display = _glfw.wl.display;
  1156. errno = 0;
  1157. EVDBG("starting handleEvents(%.2f)", monotonic_t_to_s_double(timeout));
  1158. while (wl_display_prepare_read(display) != 0) {
  1159. if (wl_display_dispatch_pending(display) == -1) {
  1160. abortOnFatalError(errno);
  1161. return;
  1162. }
  1163. }
  1164. // If an error different from EAGAIN happens, we have likely been
  1165. // disconnected from the Wayland session, try to handle that the best we
  1166. // can.
  1167. errno = 0;
  1168. if (wl_display_flush(display) < 0 && errno != EAGAIN)
  1169. {
  1170. wl_display_cancel_read(display);
  1171. abortOnFatalError(errno);
  1172. return;
  1173. }
  1174. // we pass in wayland_read_events to ensure that the above wl_display_prepare_read call
  1175. // is followed by either wl_display_cancel_read or wl_display_read_events
  1176. // before any events/timers are dispatched. This allows other wayland functions
  1177. // to be called in the event/timer handlers without causing a deadlock
  1178. bool display_read_ok = pollForEvents(&_glfw.wl.eventLoopData, timeout, wayland_read_events);
  1179. EVDBG("display_read_ok: %d", display_read_ok);
  1180. if (display_read_ok) {
  1181. int num = wl_display_dispatch_pending(display);
  1182. (void)num;
  1183. EVDBG("dispatched %d Wayland events", num);
  1184. }
  1185. glfw_ibus_dispatch(&_glfw.wl.xkb.ibus);
  1186. glfw_dbus_session_bus_dispatch();
  1187. EVDBG("other dispatch done");
  1188. if (_glfw.wl.eventLoopData.wakeup_fd_ready) check_for_wakeup_events(&_glfw.wl.eventLoopData);
  1189. }
  1190. static struct wl_cursor*
  1191. try_cursor_names(struct wl_cursor_theme* theme, int arg_count, ...) {
  1192. struct wl_cursor* ans = NULL;
  1193. va_list ap;
  1194. va_start(ap, arg_count);
  1195. for (int i = 0; i < arg_count && !ans; i++) {
  1196. const char *name = va_arg(ap, const char *);
  1197. ans = wl_cursor_theme_get_cursor(theme, name);
  1198. }
  1199. va_end(ap);
  1200. return ans;
  1201. }
  1202. struct wl_cursor* _glfwLoadCursor(GLFWCursorShape shape, struct wl_cursor_theme* theme)
  1203. {
  1204. static bool warnings[GLFW_INVALID_CURSOR] = {0};
  1205. if (!theme) return NULL;
  1206. #define NUMARGS(...) (sizeof((const char*[]){__VA_ARGS__})/sizeof(const char*))
  1207. #define C(name, ...) case name: { \
  1208. ans = try_cursor_names(theme, NUMARGS(__VA_ARGS__), __VA_ARGS__); \
  1209. if (!ans && !warnings[name]) {\
  1210. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: Could not find standard cursor: %s", #name); \
  1211. warnings[name] = true; \
  1212. } \
  1213. break; }
  1214. struct wl_cursor* ans = NULL;
  1215. switch (shape)
  1216. {
  1217. /* start glfw to xc mapping (auto generated by gen-key-constants.py do not edit) */
  1218. C(GLFW_DEFAULT_CURSOR, "default", "left_ptr");
  1219. C(GLFW_TEXT_CURSOR, "text", "xterm", "ibeam");
  1220. C(GLFW_POINTER_CURSOR, "pointing_hand", "pointer", "hand2", "hand");
  1221. C(GLFW_HELP_CURSOR, "help", "question_arrow", "whats_this");
  1222. C(GLFW_WAIT_CURSOR, "wait", "clock", "watch");
  1223. C(GLFW_PROGRESS_CURSOR, "progress", "half-busy", "left_ptr_watch");
  1224. C(GLFW_CROSSHAIR_CURSOR, "crosshair", "tcross");
  1225. C(GLFW_CELL_CURSOR, "cell", "plus", "cross");
  1226. C(GLFW_VERTICAL_TEXT_CURSOR, "vertical-text");
  1227. C(GLFW_MOVE_CURSOR, "move", "fleur", "pointer-move");
  1228. C(GLFW_E_RESIZE_CURSOR, "e-resize", "right_side");
  1229. C(GLFW_NE_RESIZE_CURSOR, "ne-resize", "top_right_corner");
  1230. C(GLFW_NW_RESIZE_CURSOR, "nw-resize", "top_left_corner");
  1231. C(GLFW_N_RESIZE_CURSOR, "n-resize", "top_side");
  1232. C(GLFW_SE_RESIZE_CURSOR, "se-resize", "bottom_right_corner");
  1233. C(GLFW_SW_RESIZE_CURSOR, "sw-resize", "bottom_left_corner");
  1234. C(GLFW_S_RESIZE_CURSOR, "s-resize", "bottom_side");
  1235. C(GLFW_W_RESIZE_CURSOR, "w-resize", "left_side");
  1236. C(GLFW_EW_RESIZE_CURSOR, "ew-resize", "sb_h_double_arrow", "split_h");
  1237. C(GLFW_NS_RESIZE_CURSOR, "ns-resize", "sb_v_double_arrow", "split_v");
  1238. C(GLFW_NESW_RESIZE_CURSOR, "nesw-resize", "size_bdiag", "size-bdiag");
  1239. C(GLFW_NWSE_RESIZE_CURSOR, "nwse-resize", "size_fdiag", "size-fdiag");
  1240. C(GLFW_ZOOM_IN_CURSOR, "zoom-in", "zoom_in");
  1241. C(GLFW_ZOOM_OUT_CURSOR, "zoom-out", "zoom_out");
  1242. C(GLFW_ALIAS_CURSOR, "dnd-link");
  1243. C(GLFW_COPY_CURSOR, "dnd-copy");
  1244. C(GLFW_NOT_ALLOWED_CURSOR, "not-allowed", "forbidden", "crossed_circle");
  1245. C(GLFW_NO_DROP_CURSOR, "no-drop", "dnd-no-drop");
  1246. C(GLFW_GRAB_CURSOR, "grab", "openhand", "hand1");
  1247. C(GLFW_GRABBING_CURSOR, "grabbing", "closedhand", "dnd-none");
  1248. /* end glfw to xc mapping */
  1249. case GLFW_INVALID_CURSOR:
  1250. break;
  1251. }
  1252. return ans;
  1253. #undef NUMARGS
  1254. #undef C
  1255. }
  1256. //////////////////////////////////////////////////////////////////////////
  1257. ////// GLFW platform API //////
  1258. //////////////////////////////////////////////////////////////////////////
  1259. static bool
  1260. attach_opengl_context_to_window(_GLFWwindow *window, const _GLFWctxconfig *ctxconfig, const _GLFWfbconfig *fbconfig) {
  1261. if (ctxconfig->source == GLFW_EGL_CONTEXT_API ||
  1262. ctxconfig->source == GLFW_NATIVE_CONTEXT_API)
  1263. {
  1264. if (!_glfwInitEGL())
  1265. return false;
  1266. if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig))
  1267. return false;
  1268. }
  1269. else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API)
  1270. {
  1271. if (!_glfwInitOSMesa())
  1272. return false;
  1273. if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig))
  1274. return false;
  1275. }
  1276. return true;
  1277. }
  1278. int _glfwPlatformCreateWindow(
  1279. _GLFWwindow* window, const _GLFWwndconfig* wndconfig, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig,
  1280. const GLFWLayerShellConfig *lsc
  1281. ) {
  1282. window->wl.layer_shell.config = lsc ? *lsc : (GLFWLayerShellConfig){0};
  1283. csd_initialize_metrics(window);
  1284. window->wl.transparent = fbconfig->transparent;
  1285. strncpy(window->wl.appId, wndconfig->wl.appId, sizeof(window->wl.appId));
  1286. window->swaps_disallowed = true;
  1287. if (!createSurface(window, wndconfig)) return false;
  1288. if (wndconfig->title) window->wl.title = _glfw_strdup(wndconfig->title);
  1289. if (wndconfig->maximized) window->wl.maximize_on_first_show = true;
  1290. if (wndconfig->visible) {
  1291. if (!create_window_desktop_surface(window)) return false;
  1292. window->wl.visible = true;
  1293. } else {
  1294. window->wl.visible = false;
  1295. window->wl.xdg.surface = NULL;
  1296. window->wl.xdg.toplevel = NULL;
  1297. window->wl.layer_shell.zwlr_layer_surface_v1 = NULL;
  1298. }
  1299. window->wl.currentCursor = NULL;
  1300. // Don't set window->wl.cursorTheme to NULL here.
  1301. window->wl.monitors = calloc(1, sizeof(_GLFWmonitor*));
  1302. window->wl.monitorsCount = 0;
  1303. window->wl.monitorsSize = 1;
  1304. // looping till window fully created attaches a single pixel buffer to the window,
  1305. // this cannot be done once a OpenGL context is created for the window. So first loop
  1306. // and only then create the OpenGL context.
  1307. if (window->wl.visible) loop_till_window_fully_created(window);
  1308. debug("Creating OpenGL context and attaching it to window\n");
  1309. if (ctxconfig->client != GLFW_NO_API) attach_opengl_context_to_window(window, ctxconfig, fbconfig);
  1310. return true;
  1311. }
  1312. void _glfwPlatformDestroyWindow(_GLFWwindow* window)
  1313. {
  1314. if (window == _glfw.wl.pointerFocus)
  1315. {
  1316. _glfw.wl.pointerFocus = NULL;
  1317. _glfwInputCursorEnter(window, false);
  1318. }
  1319. if (window->id == _glfw.wl.keyboardFocusId)
  1320. {
  1321. _glfw.wl.keyboardFocusId = 0;
  1322. _glfwInputWindowFocus(window, false);
  1323. }
  1324. if (window->id == _glfw.wl.keyRepeatInfo.keyboardFocusId) {
  1325. _glfw.wl.keyRepeatInfo.keyboardFocusId = 0;
  1326. }
  1327. if (window->wl.temp_buffer_used_during_window_creation)
  1328. wl_buffer_destroy(window->wl.temp_buffer_used_during_window_creation);
  1329. if (window->wl.wp_fractional_scale_v1)
  1330. wp_fractional_scale_v1_destroy(window->wl.wp_fractional_scale_v1);
  1331. if (window->wl.wp_viewport)
  1332. wp_viewport_destroy(window->wl.wp_viewport);
  1333. if (window->wl.org_kde_kwin_blur)
  1334. org_kde_kwin_blur_release(window->wl.org_kde_kwin_blur);
  1335. if (window->context.destroy)
  1336. window->context.destroy(window);
  1337. csd_free_all_resources(window);
  1338. if (window->wl.xdg.decoration)
  1339. zxdg_toplevel_decoration_v1_destroy(window->wl.xdg.decoration);
  1340. if (window->wl.native)
  1341. wl_egl_window_destroy(window->wl.native);
  1342. if (window->wl.xdg.toplevel)
  1343. xdg_toplevel_destroy(window->wl.xdg.toplevel);
  1344. if (window->wl.xdg.surface)
  1345. xdg_surface_destroy(window->wl.xdg.surface);
  1346. if (window->wl.layer_shell.zwlr_layer_surface_v1)
  1347. zwlr_layer_surface_v1_destroy(window->wl.layer_shell.zwlr_layer_surface_v1);
  1348. if (window->wl.surface)
  1349. wl_surface_destroy(window->wl.surface);
  1350. free(window->wl.title);
  1351. free(window->wl.monitors);
  1352. if (window->wl.frameCallbackData.current_wl_callback)
  1353. wl_callback_destroy(window->wl.frameCallbackData.current_wl_callback);
  1354. }
  1355. void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
  1356. {
  1357. if (window->wl.title) {
  1358. if (title && strcmp(title, window->wl.title) == 0) return;
  1359. free(window->wl.title);
  1360. } else if (!title) return;
  1361. // Wayland cannot handle requests larger than ~8200 bytes. Sending
  1362. // one causes an abort(). Since titles this large are meaningless anyway
  1363. // ensure they do not happen.
  1364. window->wl.title = utf_8_strndup(title, 2048);
  1365. if (window->wl.xdg.toplevel) {
  1366. xdg_toplevel_set_title(window->wl.xdg.toplevel, window->wl.title);
  1367. csd_change_title(window);
  1368. commit_window_surface_if_safe(window);
  1369. }
  1370. }
  1371. void
  1372. _glfwPlatformSetWindowIcon(_GLFWwindow* window, int count, const GLFWimage* images) {
  1373. if (!_glfw.wl.xdg_toplevel_icon_manager_v1) {
  1374. static bool warned_once = false;
  1375. if (!warned_once) {
  1376. _glfwInputError(GLFW_FEATURE_UNAVAILABLE, "Wayland: The compositor does not support changing window icons");
  1377. warned_once = true;
  1378. }
  1379. return;
  1380. }
  1381. if (!count) {
  1382. xdg_toplevel_icon_manager_v1_set_icon(_glfw.wl.xdg_toplevel_icon_manager_v1, window->wl.xdg.toplevel, NULL);
  1383. return;
  1384. }
  1385. struct wl_buffer* *buffers = malloc(sizeof(struct wl_buffer*) * count);
  1386. if (!buffers) return;
  1387. size_t total_data_size = 0;
  1388. for (int i = 0; i < count; i++) total_data_size += images[i].width * images[i].height * 4;
  1389. int fd = createAnonymousFile(total_data_size);
  1390. if (fd < 0) {
  1391. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: Creating a buffer file for %ld B failed: %s", (long)total_data_size, strerror(errno));
  1392. free(buffers);
  1393. return;
  1394. }
  1395. unsigned char *data = mmap(NULL, total_data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  1396. if (data == MAP_FAILED) {
  1397. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: mmap failed: %s", strerror(errno));
  1398. free(buffers);
  1399. close(fd);
  1400. return;
  1401. }
  1402. struct wl_shm_pool* pool = wl_shm_create_pool(_glfw.wl.shm, fd, total_data_size);
  1403. struct xdg_toplevel_icon_v1 *icon = xdg_toplevel_icon_manager_v1_create_icon(_glfw.wl.xdg_toplevel_icon_manager_v1);
  1404. size_t pos = 0;
  1405. for (int i = 0; i < count; i++) {
  1406. const size_t sz = images[i].width * images[i].height * 4;
  1407. convert_glfw_image_to_wayland_image(images + i, data + pos);
  1408. buffers[i] = wl_shm_pool_create_buffer(
  1409. pool, pos, images[i].width, images[i].height, images[i].width * 4, WL_SHM_FORMAT_ARGB8888);
  1410. xdg_toplevel_icon_v1_add_buffer(icon, buffers[i], 1);
  1411. pos += sz;
  1412. }
  1413. xdg_toplevel_icon_manager_v1_set_icon(_glfw.wl.xdg_toplevel_icon_manager_v1, window->wl.xdg.toplevel, icon);
  1414. xdg_toplevel_icon_v1_destroy(icon);
  1415. for (int i = 0; i < count; i++) wl_buffer_destroy(buffers[i]);
  1416. free(buffers);
  1417. wl_shm_pool_destroy(pool);
  1418. munmap(data, total_data_size);
  1419. close(fd);
  1420. }
  1421. void _glfwPlatformGetWindowPos(_GLFWwindow* window UNUSED, int* xpos UNUSED, int* ypos UNUSED)
  1422. {
  1423. // A Wayland client is not aware of its position, so just warn and leave it
  1424. // as (0, 0)
  1425. static bool warned_once = false;
  1426. if (!warned_once) {
  1427. _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
  1428. "Wayland: The platform does not provide the window position");
  1429. warned_once = true;
  1430. }
  1431. }
  1432. void _glfwPlatformSetWindowPos(_GLFWwindow* window UNUSED, int xpos UNUSED, int ypos UNUSED)
  1433. {
  1434. // A Wayland client can not set its position, so just warn
  1435. _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
  1436. "Wayland: The platform does not support setting the window position");
  1437. }
  1438. void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
  1439. {
  1440. if (width)
  1441. *width = window->wl.width;
  1442. if (height)
  1443. *height = window->wl.height;
  1444. }
  1445. void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
  1446. {
  1447. if (is_layer_shell(window)) {
  1448. _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
  1449. "Wayland: Resizing of layer shell surfaces is not supported");
  1450. return;
  1451. }
  1452. if (width != window->wl.width || height != window->wl.height) {
  1453. window->wl.user_requested_content_size.width = width;
  1454. window->wl.user_requested_content_size.height = height;
  1455. int32_t w = 0, h = 0;
  1456. csd_set_window_geometry(window, &w, &h);
  1457. window->wl.width = w; window->wl.height = h;
  1458. resizeFramebuffer(window);
  1459. csd_set_visible(window, true); // resizes the csd iff the window currently has csd
  1460. commit_window_surface_if_safe(window);
  1461. inform_compositor_of_window_geometry(window, "SetWindowSize");
  1462. }
  1463. }
  1464. void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window,
  1465. int minwidth, int minheight,
  1466. int maxwidth, int maxheight)
  1467. {
  1468. if (window->wl.xdg.toplevel)
  1469. {
  1470. if (minwidth == GLFW_DONT_CARE || minheight == GLFW_DONT_CARE)
  1471. minwidth = minheight = 0;
  1472. if (maxwidth == GLFW_DONT_CARE || maxheight == GLFW_DONT_CARE)
  1473. maxwidth = maxheight = 0;
  1474. xdg_toplevel_set_min_size(window->wl.xdg.toplevel, minwidth, minheight);
  1475. xdg_toplevel_set_max_size(window->wl.xdg.toplevel, maxwidth, maxheight);
  1476. commit_window_surface_if_safe(window);
  1477. }
  1478. }
  1479. void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window UNUSED,
  1480. int numer UNUSED, int denom UNUSED)
  1481. {
  1482. // TODO: find out how to trigger a resize.
  1483. // The actual limits are checked in the xdg_toplevel::configure handler.
  1484. _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
  1485. "Wayland: Window aspect ratio not yet implemented");
  1486. }
  1487. void _glfwPlatformSetWindowSizeIncrements(_GLFWwindow* window UNUSED,
  1488. int widthincr UNUSED, int heightincr UNUSED)
  1489. {
  1490. // TODO: find out how to trigger a resize.
  1491. // The actual limits are checked in the xdg_toplevel::configure handler.
  1492. }
  1493. void _glfwPlatformGetFramebufferSize(_GLFWwindow* window,
  1494. int* width, int* height)
  1495. {
  1496. _glfwPlatformGetWindowSize(window, width, height);
  1497. double fscale = _glfwWaylandWindowScale(window);
  1498. if (width)
  1499. *width = (int)round(*width * fscale);
  1500. if (height)
  1501. *height = (int)round(*height * fscale);
  1502. }
  1503. void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
  1504. int* left, int* top,
  1505. int* right, int* bottom)
  1506. {
  1507. if (window->decorated && !window->monitor && !window->wl.decorations.serverSide)
  1508. {
  1509. if (top)
  1510. *top = window->wl.decorations.metrics.top - window->wl.decorations.metrics.visible_titlebar_height;
  1511. if (left)
  1512. *left = window->wl.decorations.metrics.width;
  1513. if (right)
  1514. *right = window->wl.decorations.metrics.width;
  1515. if (bottom)
  1516. *bottom = window->wl.decorations.metrics.width;
  1517. }
  1518. }
  1519. void _glfwPlatformGetWindowContentScale(_GLFWwindow* window,
  1520. float* xscale, float* yscale)
  1521. {
  1522. float fscale = (float)_glfwWaylandWindowScale(window);
  1523. if (xscale)
  1524. *xscale = fscale;
  1525. if (yscale)
  1526. *yscale = fscale;
  1527. }
  1528. monotonic_t _glfwPlatformGetDoubleClickInterval(_GLFWwindow* window UNUSED)
  1529. {
  1530. return ms_to_monotonic_t(500ll);
  1531. }
  1532. void _glfwPlatformIconifyWindow(_GLFWwindow* window)
  1533. {
  1534. if (window->wl.xdg.toplevel) {
  1535. if (window->wl.wm_capabilities.minimize) xdg_toplevel_set_minimized(window->wl.xdg.toplevel);
  1536. else _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland compositor does not support minimizing windows");
  1537. }
  1538. }
  1539. void _glfwPlatformRestoreWindow(_GLFWwindow* window)
  1540. {
  1541. if (window->wl.xdg.toplevel)
  1542. {
  1543. if (window->monitor)
  1544. xdg_toplevel_unset_fullscreen(window->wl.xdg.toplevel);
  1545. if (window->wl.current.toplevel_states & TOPLEVEL_STATE_MAXIMIZED)
  1546. xdg_toplevel_unset_maximized(window->wl.xdg.toplevel);
  1547. // There is no way to unset minimized, or even to know if we are
  1548. // minimized, so there is nothing to do in this case.
  1549. }
  1550. _glfwInputWindowMonitor(window, NULL);
  1551. }
  1552. void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
  1553. {
  1554. if (window->wl.xdg.toplevel) {
  1555. if (window->wl.wm_capabilities.maximize) xdg_toplevel_set_maximized(window->wl.xdg.toplevel);
  1556. else _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland compositor does not support maximizing windows");
  1557. }
  1558. }
  1559. void _glfwPlatformShowWindow(_GLFWwindow* window)
  1560. {
  1561. if (!window->wl.visible) {
  1562. if (!window->wl.created) {
  1563. create_window_desktop_surface(window);
  1564. window->wl.visible = true;
  1565. } else {
  1566. // workaround for kwin layer shell bug: https://bugs.kde.org/show_bug.cgi?id=503121
  1567. if (is_layer_shell(window)) layer_set_properties(window, false, window->wl.width, window->wl.height);
  1568. window->wl.visible = true;
  1569. commit_window_surface(window);
  1570. }
  1571. debug("Window %llu mapped waiting for configure event from compositor\n", window->id);
  1572. }
  1573. }
  1574. void _glfwPlatformHideWindow(_GLFWwindow* window)
  1575. {
  1576. if (!window->wl.visible) return;
  1577. wl_surface_attach(window->wl.surface, NULL, 0, 0);
  1578. window->wl.once.surface_configured = false;
  1579. window->swaps_disallowed = true;
  1580. window->wl.visible = false;
  1581. commit_window_surface(window);
  1582. debug("Window %llu unmapped\n", window->id);
  1583. }
  1584. bool
  1585. _glfwPlatformSetLayerShellConfig(_GLFWwindow* window, const GLFWLayerShellConfig *value) {
  1586. if (!is_layer_shell(window)) return false;
  1587. if (value) window->wl.layer_shell.config = *value;
  1588. uint32_t width, height;
  1589. calculate_layer_size(window, &width, &height);
  1590. layer_set_properties(window, false, width, height);
  1591. commit_window_surface(window);
  1592. return true;
  1593. }
  1594. static void
  1595. request_attention(GLFWwindow *window, const char *token, void *data UNUSED) {
  1596. if (window && token && token[0] && _glfw.wl.xdg_activation_v1) xdg_activation_v1_activate(_glfw.wl.xdg_activation_v1, token, ((_GLFWwindow*)window)->wl.surface);
  1597. }
  1598. static bool
  1599. has_activation_in_flight(_GLFWwindow* window, GLFWactivationcallback callback) {
  1600. for (size_t i = 0; i < _glfw.wl.activation_requests.sz; i++) {
  1601. glfw_wl_xdg_activation_request *r = _glfw.wl.activation_requests.array + i;
  1602. if (r->window_id == window->id && r->callback == callback) return true;
  1603. }
  1604. return false;
  1605. }
  1606. void _glfwPlatformRequestWindowAttention(_GLFWwindow* window) {
  1607. if (!has_activation_in_flight(window, request_attention)) get_activation_token(window, 0, request_attention, NULL);
  1608. }
  1609. int _glfwPlatformWindowBell(_GLFWwindow* window UNUSED)
  1610. {
  1611. // TODO: Use an actual Wayland API to implement this when one becomes available
  1612. return false;
  1613. }
  1614. static void
  1615. focus_window(GLFWwindow *window, const char *token, void *data UNUSED) {
  1616. if (!window) return;
  1617. if (token && token[0] && _glfw.wl.xdg_activation_v1) xdg_activation_v1_activate(_glfw.wl.xdg_activation_v1, token, ((_GLFWwindow*)window)->wl.surface);
  1618. else {
  1619. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: Window focus request via xdg-activation protocol was denied or is unsupported by the compositor. Use a better compositor.");
  1620. }
  1621. }
  1622. void _glfwPlatformFocusWindow(_GLFWwindow* window UNUSED)
  1623. {
  1624. // Attempt to focus the window by using the activation protocol, whether it works
  1625. // is entirely compositor dependent and as we all know Wayland and its ecosystem is
  1626. // the product of morons.
  1627. if (_glfw.wl.input_serial && !has_activation_in_flight(window, focus_window)) get_activation_token(window, _glfw.wl.input_serial, focus_window, NULL);
  1628. }
  1629. void _glfwPlatformSetWindowMonitor(_GLFWwindow* window,
  1630. _GLFWmonitor* monitor,
  1631. int xpos UNUSED, int ypos UNUSED,
  1632. int width UNUSED, int height UNUSED,
  1633. int refreshRate UNUSED)
  1634. {
  1635. setFullscreen(window, monitor, monitor != NULL);
  1636. _glfwInputWindowMonitor(window, monitor);
  1637. }
  1638. int _glfwPlatformWindowFocused(_GLFWwindow* window)
  1639. {
  1640. return _glfw.wl.keyboardFocusId == (window ? window->id : 0);
  1641. }
  1642. int _glfwPlatformWindowOccluded(_GLFWwindow* window UNUSED)
  1643. {
  1644. #ifdef XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION
  1645. return (window->wl.current.toplevel_states & TOPLEVEL_STATE_SUSPENDED) != 0;
  1646. #endif
  1647. return false;
  1648. }
  1649. int _glfwPlatformWindowIconified(_GLFWwindow* window UNUSED)
  1650. {
  1651. // xdg-shell doesn’t give any way to request whether a surface is
  1652. // iconified.
  1653. return false;
  1654. }
  1655. int _glfwPlatformWindowVisible(_GLFWwindow* window)
  1656. {
  1657. return window->wl.visible;
  1658. }
  1659. int _glfwPlatformWindowMaximized(_GLFWwindow* window)
  1660. {
  1661. return window->wl.current.toplevel_states & TOPLEVEL_STATE_MAXIMIZED;
  1662. }
  1663. int _glfwPlatformWindowHovered(_GLFWwindow* window)
  1664. {
  1665. return window->wl.hovered;
  1666. }
  1667. int _glfwPlatformFramebufferTransparent(_GLFWwindow* window)
  1668. {
  1669. return window->wl.transparent;
  1670. }
  1671. void _glfwPlatformSetWindowResizable(_GLFWwindow* window UNUSED, bool enabled UNUSED)
  1672. {
  1673. // TODO
  1674. _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
  1675. "Wayland: Window attribute setting not implemented yet");
  1676. }
  1677. void _glfwPlatformSetWindowFloating(_GLFWwindow* window UNUSED, bool enabled UNUSED)
  1678. {
  1679. // TODO
  1680. _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
  1681. "Wayland: Window attribute setting not implemented yet");
  1682. }
  1683. void _glfwPlatformSetWindowMousePassthrough(_GLFWwindow* window, bool enabled)
  1684. {
  1685. if (enabled)
  1686. {
  1687. struct wl_region* region = wl_compositor_create_region(_glfw.wl.compositor);
  1688. wl_surface_set_input_region(window->wl.surface, region);
  1689. wl_region_destroy(region);
  1690. }
  1691. else
  1692. wl_surface_set_input_region(window->wl.surface, 0);
  1693. commit_window_surface_if_safe(window);
  1694. }
  1695. float _glfwPlatformGetWindowOpacity(_GLFWwindow* window UNUSED)
  1696. {
  1697. return 1.f;
  1698. }
  1699. void _glfwPlatformSetWindowOpacity(_GLFWwindow* window UNUSED, float opacity UNUSED)
  1700. {
  1701. _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
  1702. "Wayland: The platform does not support setting the window opacity");
  1703. }
  1704. void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window UNUSED, bool enabled UNUSED)
  1705. {
  1706. // This is handled in relativePointerHandleRelativeMotion
  1707. }
  1708. bool _glfwPlatformRawMouseMotionSupported(void)
  1709. {
  1710. return true;
  1711. }
  1712. void _glfwPlatformPollEvents(void)
  1713. {
  1714. wl_display_dispatch_pending(_glfw.wl.display);
  1715. handleEvents(0);
  1716. }
  1717. void _glfwPlatformWaitEvents(void)
  1718. {
  1719. monotonic_t timeout = wl_display_dispatch_pending(_glfw.wl.display) > 0 ? 0 : -1;
  1720. handleEvents(timeout);
  1721. }
  1722. void _glfwPlatformWaitEventsTimeout(monotonic_t timeout)
  1723. {
  1724. if (wl_display_dispatch_pending(_glfw.wl.display) > 0) timeout = 0;
  1725. handleEvents(timeout);
  1726. }
  1727. void _glfwPlatformPostEmptyEvent(void)
  1728. {
  1729. wakeupEventLoop(&_glfw.wl.eventLoopData);
  1730. }
  1731. void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
  1732. {
  1733. if (xpos)
  1734. *xpos = window->wl.cursorPosX;
  1735. if (ypos)
  1736. *ypos = window->wl.cursorPosY;
  1737. }
  1738. static bool isPointerLocked(_GLFWwindow* window);
  1739. void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
  1740. {
  1741. if (isPointerLocked(window))
  1742. {
  1743. zwp_locked_pointer_v1_set_cursor_position_hint(
  1744. window->wl.pointerLock.lockedPointer,
  1745. wl_fixed_from_double(x), wl_fixed_from_double(y));
  1746. commit_window_surface_if_safe(window);
  1747. }
  1748. }
  1749. void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode UNUSED)
  1750. {
  1751. _glfwPlatformSetCursor(window, window->wl.currentCursor);
  1752. }
  1753. const char* _glfwPlatformGetNativeKeyName(int native_key)
  1754. {
  1755. return glfw_xkb_keysym_name(native_key);
  1756. }
  1757. int _glfwPlatformGetNativeKeyForKey(uint32_t key)
  1758. {
  1759. return glfw_xkb_sym_for_key(key);
  1760. }
  1761. int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
  1762. const GLFWimage* image,
  1763. int xhot, int yhot, int count UNUSED)
  1764. {
  1765. cursor->wl.buffer = createShmBuffer(image, false, true);
  1766. if (!cursor->wl.buffer)
  1767. return false;
  1768. cursor->wl.width = image->width;
  1769. cursor->wl.height = image->height;
  1770. cursor->wl.xhot = xhot;
  1771. cursor->wl.yhot = yhot;
  1772. cursor->wl.scale = -1;
  1773. cursor->wl.shape = GLFW_INVALID_CURSOR;
  1774. return true;
  1775. }
  1776. int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, GLFWCursorShape shape)
  1777. {
  1778. // Don't actually load the cursor at this point,
  1779. // because there's not enough info to be properly HiDPI aware.
  1780. cursor->wl.cursor = NULL;
  1781. cursor->wl.currentImage = 0;
  1782. cursor->wl.scale = 0;
  1783. cursor->wl.shape = shape;
  1784. return true;
  1785. }
  1786. void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
  1787. {
  1788. // If it's a standard cursor we don't need to do anything here
  1789. if (cursor->wl.cursor)
  1790. return;
  1791. if (cursor->wl.buffer)
  1792. wl_buffer_destroy(cursor->wl.buffer);
  1793. }
  1794. static void relativePointerHandleRelativeMotion(void* data,
  1795. struct zwp_relative_pointer_v1* pointer UNUSED,
  1796. uint32_t timeHi UNUSED,
  1797. uint32_t timeLo UNUSED,
  1798. wl_fixed_t dx,
  1799. wl_fixed_t dy,
  1800. wl_fixed_t dxUnaccel,
  1801. wl_fixed_t dyUnaccel)
  1802. {
  1803. _GLFWwindow* window = data;
  1804. double xpos = window->virtualCursorPosX;
  1805. double ypos = window->virtualCursorPosY;
  1806. if (window->cursorMode != GLFW_CURSOR_DISABLED)
  1807. return;
  1808. if (window->rawMouseMotion)
  1809. {
  1810. xpos += wl_fixed_to_double(dxUnaccel);
  1811. ypos += wl_fixed_to_double(dyUnaccel);
  1812. }
  1813. else
  1814. {
  1815. xpos += wl_fixed_to_double(dx);
  1816. ypos += wl_fixed_to_double(dy);
  1817. }
  1818. _glfwInputCursorPos(window, xpos, ypos);
  1819. }
  1820. static const struct zwp_relative_pointer_v1_listener relativePointerListener = {
  1821. relativePointerHandleRelativeMotion
  1822. };
  1823. static void lockedPointerHandleLocked(void* data UNUSED,
  1824. struct zwp_locked_pointer_v1* lockedPointer UNUSED)
  1825. {
  1826. }
  1827. static void unlockPointer(_GLFWwindow* window)
  1828. {
  1829. struct zwp_relative_pointer_v1* relativePointer =
  1830. window->wl.pointerLock.relativePointer;
  1831. struct zwp_locked_pointer_v1* lockedPointer =
  1832. window->wl.pointerLock.lockedPointer;
  1833. zwp_relative_pointer_v1_destroy(relativePointer);
  1834. zwp_locked_pointer_v1_destroy(lockedPointer);
  1835. window->wl.pointerLock.relativePointer = NULL;
  1836. window->wl.pointerLock.lockedPointer = NULL;
  1837. }
  1838. static void lockPointer(_GLFWwindow* window UNUSED);
  1839. static void lockedPointerHandleUnlocked(void* data UNUSED,
  1840. struct zwp_locked_pointer_v1* lockedPointer UNUSED)
  1841. {
  1842. }
  1843. static const struct zwp_locked_pointer_v1_listener lockedPointerListener = {
  1844. lockedPointerHandleLocked,
  1845. lockedPointerHandleUnlocked
  1846. };
  1847. static void lockPointer(_GLFWwindow* window)
  1848. {
  1849. struct zwp_relative_pointer_v1* relativePointer;
  1850. struct zwp_locked_pointer_v1* lockedPointer;
  1851. if (!_glfw.wl.relativePointerManager)
  1852. {
  1853. _glfwInputError(GLFW_PLATFORM_ERROR,
  1854. "Wayland: no relative pointer manager");
  1855. return;
  1856. }
  1857. relativePointer =
  1858. zwp_relative_pointer_manager_v1_get_relative_pointer(
  1859. _glfw.wl.relativePointerManager,
  1860. _glfw.wl.pointer);
  1861. zwp_relative_pointer_v1_add_listener(relativePointer,
  1862. &relativePointerListener,
  1863. window);
  1864. lockedPointer =
  1865. zwp_pointer_constraints_v1_lock_pointer(
  1866. _glfw.wl.pointerConstraints,
  1867. window->wl.surface,
  1868. _glfw.wl.pointer,
  1869. NULL,
  1870. ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
  1871. zwp_locked_pointer_v1_add_listener(lockedPointer,
  1872. &lockedPointerListener,
  1873. window);
  1874. window->wl.pointerLock.relativePointer = relativePointer;
  1875. window->wl.pointerLock.lockedPointer = lockedPointer;
  1876. set_cursor_surface(NULL, 0, 0, "lockPointer");
  1877. }
  1878. static bool isPointerLocked(_GLFWwindow* window)
  1879. {
  1880. return window->wl.pointerLock.lockedPointer != NULL;
  1881. }
  1882. void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
  1883. {
  1884. if (!_glfw.wl.pointer)
  1885. return;
  1886. window->wl.currentCursor = cursor;
  1887. // If we're not in the correct window just save the cursor
  1888. // the next time the pointer enters the window the cursor will change
  1889. if (window != _glfw.wl.pointerFocus || window->wl.decorations.focus != CENTRAL_WINDOW)
  1890. return;
  1891. // Unlock possible pointer lock if no longer disabled.
  1892. if (window->cursorMode != GLFW_CURSOR_DISABLED && isPointerLocked(window))
  1893. unlockPointer(window);
  1894. if (window->cursorMode == GLFW_CURSOR_NORMAL)
  1895. {
  1896. setCursorImage(window, false);
  1897. }
  1898. else if (window->cursorMode == GLFW_CURSOR_DISABLED)
  1899. {
  1900. if (!isPointerLocked(window))
  1901. lockPointer(window);
  1902. }
  1903. else if (window->cursorMode == GLFW_CURSOR_HIDDEN)
  1904. {
  1905. set_cursor_surface(NULL, 0, 0, "_glfwPlatformSetCursor");
  1906. }
  1907. }
  1908. static bool
  1909. write_all(int fd, const char *data, size_t sz) {
  1910. monotonic_t start = glfwGetTime();
  1911. size_t pos = 0;
  1912. while (pos < sz && glfwGetTime() - start < s_to_monotonic_t(2ll)) {
  1913. ssize_t ret = write(fd, data + pos, sz - pos);
  1914. if (ret < 0) {
  1915. if (errno == EAGAIN || errno == EINTR) continue;
  1916. _glfwInputError(GLFW_PLATFORM_ERROR,
  1917. "Wayland: Could not copy writing to destination fd failed with error: %s", strerror(errno));
  1918. return false;
  1919. }
  1920. if (ret > 0) {
  1921. start = glfwGetTime();
  1922. pos += ret;
  1923. }
  1924. }
  1925. return pos >= sz;
  1926. }
  1927. static void
  1928. send_clipboard_data(const _GLFWClipboardData *cd, const char *mime, int fd) {
  1929. if (strcmp(mime, "text/plain;charset=utf-8") == 0 || strcmp(mime, "UTF8_STRING") == 0 || strcmp(mime, "TEXT") == 0 || strcmp(mime, "STRING") == 0) mime = "text/plain";
  1930. GLFWDataChunk chunk = cd->get_data(mime, NULL, cd->ctype);
  1931. void *iter = chunk.iter;
  1932. if (!iter) return;
  1933. bool keep_going = true;
  1934. while (keep_going) {
  1935. chunk = cd->get_data(mime, iter, cd->ctype);
  1936. if (!chunk.sz) break;
  1937. if (!write_all(fd, chunk.data, chunk.sz)) keep_going = false;
  1938. if (chunk.free) chunk.free((void*)chunk.free_data);
  1939. }
  1940. cd->get_data(NULL, iter, cd->ctype);
  1941. }
  1942. static void _glfwSendClipboardText(void *data UNUSED, struct wl_data_source *data_source UNUSED, const char *mime_type, int fd) {
  1943. send_clipboard_data(&_glfw.clipboard, mime_type, fd);
  1944. close(fd);
  1945. }
  1946. static void _glfwSendPrimarySelectionText(void *data UNUSED, struct zwp_primary_selection_source_v1 *primary_selection_source UNUSED,
  1947. const char *mime_type, int fd) {
  1948. send_clipboard_data(&_glfw.primary, mime_type, fd);
  1949. close(fd);
  1950. }
  1951. static void
  1952. read_offer(int data_pipe, GLFWclipboardwritedatafun write_data, void *object) {
  1953. wl_display_flush(_glfw.wl.display);
  1954. struct pollfd fds;
  1955. fds.fd = data_pipe;
  1956. fds.events = POLLIN;
  1957. monotonic_t start = glfwGetTime();
  1958. #define bail(...) { \
  1959. _glfwInputError(GLFW_PLATFORM_ERROR, __VA_ARGS__); \
  1960. close(data_pipe); \
  1961. return; \
  1962. }
  1963. char buf[8192];
  1964. while (glfwGetTime() - start < s_to_monotonic_t(2ll)) {
  1965. int ret = poll(&fds, 1, 2000);
  1966. if (ret == -1) {
  1967. if (errno == EINTR) continue;
  1968. bail("Wayland: Failed to poll clipboard data from pipe with error: %s", strerror(errno));
  1969. }
  1970. if (!ret) {
  1971. bail("Wayland: Failed to read clipboard data from pipe (timed out)");
  1972. }
  1973. ret = read(data_pipe, buf, sizeof(buf));
  1974. if (ret == -1) {
  1975. if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) continue;
  1976. bail("Wayland: Failed to read clipboard data from pipe with error: %s", strerror(errno));
  1977. }
  1978. if (ret == 0) { close(data_pipe); return; }
  1979. if (!write_data(object, buf, ret)) bail("Wayland: call to write_data() failed with data from data offer");
  1980. start = glfwGetTime();
  1981. }
  1982. bail("Wayland: Failed to read clipboard data from pipe (timed out)");
  1983. #undef bail
  1984. }
  1985. typedef struct chunked_writer {
  1986. char *buf; size_t sz, cap;
  1987. } chunked_writer;
  1988. static bool
  1989. write_chunk(void *object, const char *data, size_t sz) {
  1990. chunked_writer *cw = object;
  1991. if (cw->cap < cw->sz + sz) {
  1992. cw->cap = MAX(cw->cap * 2, cw->sz + 8*sz);
  1993. cw->buf = realloc(cw->buf, cw->cap * sizeof(cw->buf[0]));
  1994. }
  1995. memcpy(cw->buf + cw->sz, data, sz);
  1996. cw->sz += sz;
  1997. return true;
  1998. }
  1999. static char*
  2000. read_offer_string(int data_pipe, size_t *sz) {
  2001. chunked_writer cw = {0};
  2002. read_offer(data_pipe, write_chunk, &cw);
  2003. if (cw.buf) {
  2004. *sz = cw.sz;
  2005. return cw.buf;
  2006. }
  2007. *sz = 0;
  2008. return NULL;
  2009. }
  2010. static void
  2011. read_clipboard_data_offer(struct wl_data_offer *data_offer, const char *mime, GLFWclipboardwritedatafun write_data, void *object) {
  2012. int pipefd[2];
  2013. if (pipe2(pipefd, O_CLOEXEC) != 0) return;
  2014. wl_data_offer_receive(data_offer, mime, pipefd[1]);
  2015. close(pipefd[1]);
  2016. read_offer(pipefd[0], write_data, object);
  2017. }
  2018. static void
  2019. read_primary_selection_offer(struct zwp_primary_selection_offer_v1 *primary_selection_offer, const char *mime, GLFWclipboardwritedatafun write_data, void *object) {
  2020. int pipefd[2];
  2021. if (pipe2(pipefd, O_CLOEXEC) != 0) return;
  2022. zwp_primary_selection_offer_v1_receive(primary_selection_offer, mime, pipefd[1]);
  2023. close(pipefd[1]);
  2024. read_offer(pipefd[0], write_data, object);
  2025. }
  2026. static char* read_data_offer(struct wl_data_offer *data_offer, const char *mime, size_t *sz) {
  2027. int pipefd[2];
  2028. if (pipe2(pipefd, O_CLOEXEC) != 0) return NULL;
  2029. wl_data_offer_receive(data_offer, mime, pipefd[1]);
  2030. close(pipefd[1]);
  2031. return read_offer_string(pipefd[0], sz);
  2032. }
  2033. static void data_source_canceled(void *data UNUSED, struct wl_data_source *wl_data_source) {
  2034. if (_glfw.wl.dataSourceForClipboard == wl_data_source) {
  2035. _glfw.wl.dataSourceForClipboard = NULL;
  2036. _glfw_free_clipboard_data(&_glfw.clipboard);
  2037. _glfwInputClipboardLost(GLFW_CLIPBOARD);
  2038. }
  2039. wl_data_source_destroy(wl_data_source);
  2040. }
  2041. static void primary_selection_source_canceled(void *data UNUSED, struct zwp_primary_selection_source_v1 *primary_selection_source) {
  2042. if (_glfw.wl.dataSourceForPrimarySelection == primary_selection_source) {
  2043. _glfw.wl.dataSourceForPrimarySelection = NULL;
  2044. _glfw_free_clipboard_data(&_glfw.primary);
  2045. _glfwInputClipboardLost(GLFW_PRIMARY_SELECTION);
  2046. }
  2047. zwp_primary_selection_source_v1_destroy(primary_selection_source);
  2048. }
  2049. // KWin aborts if we don't define these even though they are not used for copy/paste
  2050. static void dummy_data_source_target(void* data UNUSED, struct wl_data_source* wl_data_source UNUSED, const char* mime_type UNUSED) {
  2051. }
  2052. static void dummy_data_source_action(void* data UNUSED, struct wl_data_source* wl_data_source UNUSED, uint dnd_action UNUSED) {
  2053. }
  2054. static const struct wl_data_source_listener data_source_listener = {
  2055. .send = _glfwSendClipboardText,
  2056. .cancelled = data_source_canceled,
  2057. .target = dummy_data_source_target,
  2058. .action = dummy_data_source_action,
  2059. };
  2060. static const struct zwp_primary_selection_source_v1_listener primary_selection_source_listener = {
  2061. .send = _glfwSendPrimarySelectionText,
  2062. .cancelled = primary_selection_source_canceled,
  2063. };
  2064. void
  2065. destroy_data_offer(_GLFWWaylandDataOffer *offer) {
  2066. if (offer->id) {
  2067. if (offer->is_primary) zwp_primary_selection_offer_v1_destroy(offer->id);
  2068. else wl_data_offer_destroy(offer->id);
  2069. }
  2070. if (offer->mimes) {
  2071. for (size_t i = 0; i < offer->mimes_count; i++) free((char*)offer->mimes[i]);
  2072. free(offer->mimes);
  2073. }
  2074. memset(offer, 0, sizeof(_GLFWWaylandDataOffer));
  2075. }
  2076. static void prune_unclaimed_data_offers(void) {
  2077. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2078. if (_glfw.wl.dataOffers[i].id && !_glfw.wl.dataOffers[i].offer_type) {
  2079. destroy_data_offer(&_glfw.wl.dataOffers[i]);
  2080. }
  2081. }
  2082. }
  2083. static void mark_selection_offer(void *data UNUSED, struct wl_data_device *data_device UNUSED, struct wl_data_offer *data_offer)
  2084. {
  2085. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2086. if (_glfw.wl.dataOffers[i].id == data_offer) {
  2087. _glfw.wl.dataOffers[i].offer_type = CLIPBOARD;
  2088. } else if (_glfw.wl.dataOffers[i].offer_type == CLIPBOARD) {
  2089. _glfw.wl.dataOffers[i].offer_type = EXPIRED; // previous selection offer
  2090. }
  2091. }
  2092. prune_unclaimed_data_offers();
  2093. }
  2094. static void mark_primary_selection_offer(void *data UNUSED, struct zwp_primary_selection_device_v1* primary_selection_device UNUSED,
  2095. struct zwp_primary_selection_offer_v1 *primary_selection_offer) {
  2096. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2097. if (_glfw.wl.dataOffers[i].id == primary_selection_offer) {
  2098. _glfw.wl.dataOffers[i].offer_type = PRIMARY_SELECTION;
  2099. } else if (_glfw.wl.dataOffers[i].offer_type == PRIMARY_SELECTION) {
  2100. _glfw.wl.dataOffers[i].offer_type = EXPIRED; // previous selection offer
  2101. }
  2102. }
  2103. prune_unclaimed_data_offers();
  2104. }
  2105. static void
  2106. set_offer_mimetype(_GLFWWaylandDataOffer* offer, const char* mime) {
  2107. if (strcmp(mime, clipboard_mime()) == 0) {
  2108. offer->is_self_offer = true;
  2109. }
  2110. if (!offer->mimes || offer->mimes_count >= offer->mimes_capacity - 1) {
  2111. offer->mimes = realloc(offer->mimes, sizeof(char*) * (offer->mimes_capacity + 64));
  2112. if (offer->mimes) offer->mimes_capacity += 64;
  2113. else return;
  2114. }
  2115. offer->mimes[offer->mimes_count++] = _glfw_strdup(mime);
  2116. }
  2117. static void handle_offer_mimetype(void *data UNUSED, struct wl_data_offer* id, const char *mime) {
  2118. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2119. if (_glfw.wl.dataOffers[i].id == id) {
  2120. set_offer_mimetype(&_glfw.wl.dataOffers[i], mime);
  2121. break;
  2122. }
  2123. }
  2124. }
  2125. static void handle_primary_selection_offer_mimetype(void *data UNUSED, struct zwp_primary_selection_offer_v1* id, const char *mime) {
  2126. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2127. if (_glfw.wl.dataOffers[i].id == id) {
  2128. set_offer_mimetype((_GLFWWaylandDataOffer*)&_glfw.wl.dataOffers[i], mime);
  2129. break;
  2130. }
  2131. }
  2132. }
  2133. static void data_offer_source_actions(void *data UNUSED, struct wl_data_offer* id, uint32_t actions) {
  2134. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2135. if (_glfw.wl.dataOffers[i].id == id) {
  2136. _glfw.wl.dataOffers[i].source_actions = actions;
  2137. break;
  2138. }
  2139. }
  2140. }
  2141. static void data_offer_action(void *data UNUSED, struct wl_data_offer* id, uint32_t action) {
  2142. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2143. if (_glfw.wl.dataOffers[i].id == id) {
  2144. _glfw.wl.dataOffers[i].dnd_action = action;
  2145. break;
  2146. }
  2147. }
  2148. }
  2149. static const struct wl_data_offer_listener data_offer_listener = {
  2150. .offer = handle_offer_mimetype,
  2151. .source_actions = data_offer_source_actions,
  2152. .action = data_offer_action,
  2153. };
  2154. static const struct zwp_primary_selection_offer_v1_listener primary_selection_offer_listener = {
  2155. .offer = handle_primary_selection_offer_mimetype,
  2156. };
  2157. static size_t
  2158. handle_data_offer_generic(void *id, bool is_primary) {
  2159. size_t smallest_idx = SIZE_MAX, pos = 0;
  2160. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2161. if (_glfw.wl.dataOffers[i].idx && _glfw.wl.dataOffers[i].idx < smallest_idx) {
  2162. smallest_idx = _glfw.wl.dataOffers[i].idx;
  2163. pos = i;
  2164. }
  2165. if (_glfw.wl.dataOffers[i].id == NULL) {
  2166. pos = i;
  2167. goto end;
  2168. }
  2169. }
  2170. if (_glfw.wl.dataOffers[pos].id) destroy_data_offer(&_glfw.wl.dataOffers[pos]);
  2171. end:
  2172. _glfw.wl.dataOffers[pos].id = id;
  2173. _glfw.wl.dataOffers[pos].is_primary = is_primary;
  2174. _glfw.wl.dataOffers[pos].idx = ++_glfw.wl.dataOffersCounter;
  2175. return pos;
  2176. }
  2177. static void handle_data_offer(void *data UNUSED, struct wl_data_device *wl_data_device UNUSED, struct wl_data_offer *id) {
  2178. handle_data_offer_generic(id, false);
  2179. wl_data_offer_add_listener(id, &data_offer_listener, NULL);
  2180. }
  2181. static void handle_primary_selection_offer(void *data UNUSED, struct zwp_primary_selection_device_v1 *zwp_primary_selection_device_v1 UNUSED, struct zwp_primary_selection_offer_v1 *id) {
  2182. handle_data_offer_generic(id, true);
  2183. zwp_primary_selection_offer_v1_add_listener(id, &primary_selection_offer_listener, NULL);
  2184. }
  2185. static void drag_enter(void *data UNUSED, struct wl_data_device *wl_data_device UNUSED, uint32_t serial, struct wl_surface *surface, wl_fixed_t x UNUSED, wl_fixed_t y UNUSED, struct wl_data_offer *id) {
  2186. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2187. _GLFWWaylandDataOffer *d = _glfw.wl.dataOffers + i;
  2188. if (d->id == id) {
  2189. d->offer_type = DRAG_AND_DROP;
  2190. d->surface = surface;
  2191. _GLFWwindow* window = _glfw.windowListHead;
  2192. int format_priority = 0;
  2193. while (window)
  2194. {
  2195. if (window->wl.surface == surface) {
  2196. for (size_t j = 0; j < d->mimes_count; j++) {
  2197. int prio = _glfwInputDrop(window, d->mimes[j], NULL, 0);
  2198. if (prio > format_priority) d->mime_for_drop = d->mimes[j];
  2199. }
  2200. break;
  2201. }
  2202. window = window->next;
  2203. }
  2204. wl_data_offer_accept(id, serial, d->mime_for_drop);
  2205. } else if (_glfw.wl.dataOffers[i].offer_type == DRAG_AND_DROP) {
  2206. _glfw.wl.dataOffers[i].offer_type = EXPIRED; // previous drag offer
  2207. }
  2208. }
  2209. prune_unclaimed_data_offers();
  2210. }
  2211. static void drag_leave(void *data UNUSED, struct wl_data_device *wl_data_device UNUSED) {
  2212. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2213. if (_glfw.wl.dataOffers[i].offer_type == DRAG_AND_DROP) {
  2214. destroy_data_offer(&_glfw.wl.dataOffers[i]);
  2215. }
  2216. }
  2217. }
  2218. static void drop(void *data UNUSED, struct wl_data_device *wl_data_device UNUSED) {
  2219. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2220. if (_glfw.wl.dataOffers[i].offer_type == DRAG_AND_DROP && _glfw.wl.dataOffers[i].mime_for_drop) {
  2221. size_t sz = 0;
  2222. char *d = read_data_offer(_glfw.wl.dataOffers[i].id, _glfw.wl.dataOffers[i].mime_for_drop, &sz);
  2223. if (d) {
  2224. // We dont do finish as this requires version 3 for wl_data_device_manager
  2225. // which then requires more work with calling set_actions for drag and drop to function
  2226. // wl_data_offer_finish(_glfw.wl.dataOffers[i].id);
  2227. _GLFWwindow* window = _glfw.windowListHead;
  2228. while (window)
  2229. {
  2230. if (window->wl.surface == _glfw.wl.dataOffers[i].surface) {
  2231. _glfwInputDrop(window, _glfw.wl.dataOffers[i].mime_for_drop, d, sz);
  2232. break;
  2233. }
  2234. window = window->next;
  2235. }
  2236. free(d);
  2237. }
  2238. destroy_data_offer(&_glfw.wl.dataOffers[i]);
  2239. break;
  2240. }
  2241. }
  2242. }
  2243. static void motion(void *data UNUSED, struct wl_data_device *wl_data_device UNUSED, uint32_t time UNUSED, wl_fixed_t x UNUSED, wl_fixed_t y UNUSED) {
  2244. }
  2245. static const struct wl_data_device_listener data_device_listener = {
  2246. .data_offer = handle_data_offer,
  2247. .selection = mark_selection_offer,
  2248. .enter = drag_enter,
  2249. .motion = motion,
  2250. .drop = drop,
  2251. .leave = drag_leave,
  2252. };
  2253. static const struct zwp_primary_selection_device_v1_listener primary_selection_device_listener = {
  2254. .data_offer = handle_primary_selection_offer,
  2255. .selection = mark_primary_selection_offer,
  2256. };
  2257. void _glfwSetupWaylandDataDevice(void) {
  2258. _glfw.wl.dataDevice = wl_data_device_manager_get_data_device(_glfw.wl.dataDeviceManager, _glfw.wl.seat);
  2259. if (_glfw.wl.dataDevice) wl_data_device_add_listener(_glfw.wl.dataDevice, &data_device_listener, NULL);
  2260. }
  2261. void _glfwSetupWaylandPrimarySelectionDevice(void) {
  2262. _glfw.wl.primarySelectionDevice = zwp_primary_selection_device_manager_v1_get_device(_glfw.wl.primarySelectionDeviceManager, _glfw.wl.seat);
  2263. if (_glfw.wl.primarySelectionDevice) zwp_primary_selection_device_v1_add_listener(_glfw.wl.primarySelectionDevice, &primary_selection_device_listener, NULL);
  2264. }
  2265. static bool _glfwEnsureDataDevice(void) {
  2266. if (!_glfw.wl.dataDeviceManager)
  2267. {
  2268. _glfwInputError(GLFW_PLATFORM_ERROR,
  2269. "Wayland: Cannot use clipboard, data device manager is not ready");
  2270. return false;
  2271. }
  2272. if (!_glfw.wl.dataDevice)
  2273. {
  2274. if (!_glfw.wl.seat)
  2275. {
  2276. _glfwInputError(GLFW_PLATFORM_ERROR,
  2277. "Wayland: Cannot use clipboard, seat is not ready");
  2278. return false;
  2279. }
  2280. if (!_glfw.wl.dataDevice)
  2281. {
  2282. _glfwInputError(GLFW_PLATFORM_ERROR,
  2283. "Wayland: Cannot use clipboard, failed to create data device");
  2284. return false;
  2285. }
  2286. }
  2287. return true;
  2288. }
  2289. typedef void(*add_offer_func)(void*, const char *mime);
  2290. void
  2291. _glfwPlatformSetClipboard(GLFWClipboardType t) {
  2292. _GLFWClipboardData *cd = NULL;
  2293. void *data_source;
  2294. add_offer_func f;
  2295. if (t == GLFW_CLIPBOARD) {
  2296. if (!_glfwEnsureDataDevice()) return;
  2297. cd = &_glfw.clipboard;
  2298. f = (add_offer_func)wl_data_source_offer;
  2299. if (_glfw.wl.dataSourceForClipboard) wl_data_source_destroy(_glfw.wl.dataSourceForClipboard);
  2300. _glfw.wl.dataSourceForClipboard = wl_data_device_manager_create_data_source(_glfw.wl.dataDeviceManager);
  2301. if (!_glfw.wl.dataSourceForClipboard)
  2302. {
  2303. _glfwInputError(GLFW_PLATFORM_ERROR,
  2304. "Wayland: Cannot copy failed to create data source");
  2305. return;
  2306. }
  2307. wl_data_source_add_listener(_glfw.wl.dataSourceForClipboard, &data_source_listener, NULL);
  2308. data_source = _glfw.wl.dataSourceForClipboard;
  2309. } else {
  2310. if (!_glfw.wl.primarySelectionDevice) {
  2311. static bool warned_about_primary_selection_device = false;
  2312. if (!warned_about_primary_selection_device) {
  2313. _glfwInputError(GLFW_PLATFORM_ERROR,
  2314. "Wayland: Cannot copy no primary selection device available");
  2315. warned_about_primary_selection_device = true;
  2316. }
  2317. return;
  2318. }
  2319. cd = &_glfw.primary;
  2320. f = (add_offer_func)zwp_primary_selection_source_v1_offer;
  2321. if (_glfw.wl.dataSourceForPrimarySelection) zwp_primary_selection_source_v1_destroy(_glfw.wl.dataSourceForPrimarySelection);
  2322. _glfw.wl.dataSourceForPrimarySelection = zwp_primary_selection_device_manager_v1_create_source(_glfw.wl.primarySelectionDeviceManager);
  2323. if (!_glfw.wl.dataSourceForPrimarySelection)
  2324. {
  2325. _glfwInputError(GLFW_PLATFORM_ERROR,
  2326. "Wayland: Cannot copy failed to create primary selection source");
  2327. return;
  2328. }
  2329. zwp_primary_selection_source_v1_add_listener(_glfw.wl.dataSourceForPrimarySelection, &primary_selection_source_listener, NULL);
  2330. data_source = _glfw.wl.dataSourceForPrimarySelection;
  2331. }
  2332. f(data_source, clipboard_mime());
  2333. for (size_t i = 0; i < cd->num_mime_types; i++) {
  2334. if (strcmp(cd->mime_types[i], "text/plain") == 0) {
  2335. f(data_source, "TEXT");
  2336. f(data_source, "STRING");
  2337. f(data_source, "UTF8_STRING");
  2338. f(data_source, "text/plain;charset=utf-8");
  2339. }
  2340. f(data_source, cd->mime_types[i]);
  2341. }
  2342. if (t == GLFW_CLIPBOARD) {
  2343. // According to some interpretations of the Wayland spec only the application that has keyboard focus can set the clipboard.
  2344. // Hurray for the Wayland nanny state!
  2345. //
  2346. // However in wl-roots based compositors, using the serial from the keyboard enter event doesn't work. No clue what
  2347. // the correct serial to use here is. Given this Wayland there probably isn't one. What a joke.
  2348. // Bug report: https://github.com/kovidgoyal/kitty/issues/6890
  2349. // Ironically one of the contributors to wl_roots claims the keyboard enter serial is the correct one to use:
  2350. // https://emersion.fr/blog/2020/wayland-clipboard-drag-and-drop/
  2351. // The Wayland spec itself says "serial number of the event that triggered this request"
  2352. // https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_data_device
  2353. // So who the fuck knows. Just use the latest received serial and ask anybody that uses Wayland
  2354. // to get their head examined.
  2355. wl_data_device_set_selection(_glfw.wl.dataDevice, _glfw.wl.dataSourceForClipboard, _glfw.wl.serial);
  2356. } else {
  2357. // According to the Wayland spec we can only set the primary selection in response to a pointer button event
  2358. // Hurray for the Wayland nanny state!
  2359. zwp_primary_selection_device_v1_set_selection(
  2360. _glfw.wl.primarySelectionDevice, _glfw.wl.dataSourceForPrimarySelection, _glfw.wl.pointer_serial);
  2361. }
  2362. }
  2363. static bool
  2364. offer_has_mime(const _GLFWWaylandDataOffer *d, const char *mime) {
  2365. for (unsigned i = 0; i < d->mimes_count; i++) {
  2366. if (strcmp(d->mimes[i], mime) == 0) return true;
  2367. }
  2368. return false;
  2369. }
  2370. static const char*
  2371. plain_text_mime_for_offer(const _GLFWWaylandDataOffer *d) {
  2372. #define A(x) if (offer_has_mime(d, x)) return x;
  2373. A("text/plain;charset=utf-8");
  2374. A("text/plain");
  2375. A("UTF8_STRING");
  2376. A("STRING");
  2377. A("TEXT");
  2378. #undef A
  2379. return NULL;
  2380. }
  2381. void
  2382. _glfwPlatformGetClipboard(GLFWClipboardType clipboard_type, const char* mime_type, GLFWclipboardwritedatafun write_data, void *object) {
  2383. _GLFWWaylandOfferType offer_type = clipboard_type == GLFW_PRIMARY_SELECTION ? PRIMARY_SELECTION : CLIPBOARD;
  2384. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2385. _GLFWWaylandDataOffer *d = _glfw.wl.dataOffers + i;
  2386. if (d->id && d->offer_type == offer_type) {
  2387. if (d->is_self_offer) {
  2388. write_data(object, NULL, 1);
  2389. return;
  2390. }
  2391. if (mime_type == NULL) {
  2392. bool ok = true;
  2393. for (size_t o = 0; o < d->mimes_count; o++) {
  2394. const char *q = d->mimes[o];
  2395. if (strchr(d->mimes[0], '/')) {
  2396. if (strcmp(q, clipboard_mime()) == 0) continue;
  2397. if (strcmp(q, "text/plain;charset=utf-8") == 0) q = "text/plain";
  2398. } else {
  2399. if (strcmp(q, "UTF8_STRING") == 0 || strcmp(q, "STRING") == 0 || strcmp(q, "TEXT") == 0) q = "text/plain";
  2400. }
  2401. if (ok) ok = write_data(object, q, strlen(q));
  2402. }
  2403. return;
  2404. }
  2405. if (strcmp(mime_type, "text/plain") == 0) {
  2406. mime_type = plain_text_mime_for_offer(d);
  2407. if (!mime_type) return;
  2408. }
  2409. if (d->is_primary) {
  2410. read_primary_selection_offer(d->id, mime_type, write_data, object);
  2411. } else {
  2412. read_clipboard_data_offer(d->id, mime_type, write_data, object);
  2413. }
  2414. break;
  2415. }
  2416. }
  2417. }
  2418. EGLenum _glfwPlatformGetEGLPlatform(EGLint** attribs UNUSED)
  2419. {
  2420. if (_glfw.egl.EXT_platform_base && _glfw.egl.EXT_platform_wayland)
  2421. return EGL_PLATFORM_WAYLAND_EXT;
  2422. else
  2423. return 0;
  2424. }
  2425. EGLNativeDisplayType _glfwPlatformGetEGLNativeDisplay(void)
  2426. {
  2427. return _glfw.wl.display;
  2428. }
  2429. EGLNativeWindowType _glfwPlatformGetEGLNativeWindow(_GLFWwindow* window)
  2430. {
  2431. return window->wl.native;
  2432. }
  2433. void _glfwPlatformGetRequiredInstanceExtensions(char** extensions)
  2434. {
  2435. if (!_glfw.vk.KHR_surface || !_glfw.vk.KHR_wayland_surface)
  2436. return;
  2437. extensions[0] = "VK_KHR_surface";
  2438. extensions[1] = "VK_KHR_wayland_surface";
  2439. }
  2440. int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance,
  2441. VkPhysicalDevice device,
  2442. uint32_t queuefamily)
  2443. {
  2444. PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR
  2445. vkGetPhysicalDeviceWaylandPresentationSupportKHR =
  2446. (PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)
  2447. vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceWaylandPresentationSupportKHR");
  2448. if (!vkGetPhysicalDeviceWaylandPresentationSupportKHR)
  2449. {
  2450. _glfwInputError(GLFW_API_UNAVAILABLE,
  2451. "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension");
  2452. return VK_NULL_HANDLE;
  2453. }
  2454. return vkGetPhysicalDeviceWaylandPresentationSupportKHR(device,
  2455. queuefamily,
  2456. _glfw.wl.display);
  2457. }
  2458. VkResult _glfwPlatformCreateWindowSurface(VkInstance instance,
  2459. _GLFWwindow* window,
  2460. const VkAllocationCallbacks* allocator,
  2461. VkSurfaceKHR* surface)
  2462. {
  2463. VkResult err;
  2464. VkWaylandSurfaceCreateInfoKHR sci;
  2465. PFN_vkCreateWaylandSurfaceKHR vkCreateWaylandSurfaceKHR;
  2466. vkCreateWaylandSurfaceKHR = (PFN_vkCreateWaylandSurfaceKHR)
  2467. vkGetInstanceProcAddr(instance, "vkCreateWaylandSurfaceKHR");
  2468. if (!vkCreateWaylandSurfaceKHR)
  2469. {
  2470. _glfwInputError(GLFW_API_UNAVAILABLE,
  2471. "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension");
  2472. return VK_ERROR_EXTENSION_NOT_PRESENT;
  2473. }
  2474. memset(&sci, 0, sizeof(sci));
  2475. sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
  2476. sci.display = _glfw.wl.display;
  2477. sci.surface = window->wl.surface;
  2478. err = vkCreateWaylandSurfaceKHR(instance, &sci, allocator, surface);
  2479. if (err)
  2480. {
  2481. _glfwInputError(GLFW_PLATFORM_ERROR,
  2482. "Wayland: Failed to create Vulkan surface: %s",
  2483. _glfwGetVulkanResultString(err));
  2484. }
  2485. return err;
  2486. }
  2487. static void
  2488. frame_handle_redraw(void *data, struct wl_callback *callback, uint32_t time UNUSED) {
  2489. _GLFWwindow* window = (_GLFWwindow*) data;
  2490. if (callback == window->wl.frameCallbackData.current_wl_callback) {
  2491. window->wl.frameCallbackData.callback(window->wl.frameCallbackData.id);
  2492. window->wl.frameCallbackData.current_wl_callback = NULL;
  2493. }
  2494. wl_callback_destroy(callback);
  2495. }
  2496. void
  2497. _glfwPlatformChangeCursorTheme(void) {
  2498. glfw_wlc_destroy();
  2499. _GLFWwindow *w = _glfw.windowListHead;
  2500. while (w) {
  2501. setCursorImage(w, true);
  2502. w = w->next;
  2503. }
  2504. }
  2505. int
  2506. _glfwPlatformSetWindowBlur(_GLFWwindow *window, int blur_radius) {
  2507. if (!window->wl.transparent) return 0;
  2508. bool has_blur = window->wl.has_blur;
  2509. bool new_has_blur = blur_radius > 0;
  2510. if (new_has_blur != has_blur) {
  2511. window->wl.has_blur = new_has_blur;
  2512. update_regions(window);
  2513. }
  2514. return has_blur ? 1 : 0;
  2515. }
  2516. //////////////////////////////////////////////////////////////////////////
  2517. ////// GLFW native API //////
  2518. //////////////////////////////////////////////////////////////////////////
  2519. GLFWAPI struct wl_display* glfwGetWaylandDisplay(void)
  2520. {
  2521. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  2522. return _glfw.wl.display;
  2523. }
  2524. GLFWAPI struct wl_surface* glfwGetWaylandWindow(GLFWwindow* handle)
  2525. {
  2526. _GLFWwindow* window = (_GLFWwindow*) handle;
  2527. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  2528. return window->wl.surface;
  2529. }
  2530. GLFWAPI void glfwWaylandActivateWindow(GLFWwindow* handle, const char *activation_token) {
  2531. _GLFWwindow* window = (_GLFWwindow*) handle;
  2532. _GLFW_REQUIRE_INIT();
  2533. if (activation_token && activation_token[0] && _glfw.wl.xdg_activation_v1) xdg_activation_v1_activate(_glfw.wl.xdg_activation_v1, activation_token, window->wl.surface);
  2534. }
  2535. GLFWAPI void glfwWaylandRunWithActivationToken(GLFWwindow *handle, GLFWactivationcallback cb, void *cb_data) {
  2536. _GLFWwindow* window = (_GLFWwindow*) handle;
  2537. _GLFW_REQUIRE_INIT();
  2538. get_activation_token(window, _glfw.wl.input_serial, cb, cb_data);
  2539. }
  2540. GLFWAPI int glfwGetNativeKeyForName(const char* keyName, bool caseSensitive) {
  2541. return glfw_xkb_keysym_from_name(keyName, caseSensitive);
  2542. }
  2543. GLFWAPI void glfwRequestWaylandFrameEvent(GLFWwindow *handle, unsigned long long id, void(*callback)(unsigned long long id)) {
  2544. _GLFWwindow* window = (_GLFWwindow*) handle;
  2545. static const struct wl_callback_listener frame_listener = { .done = frame_handle_redraw };
  2546. if (window->wl.frameCallbackData.current_wl_callback) wl_callback_destroy(window->wl.frameCallbackData.current_wl_callback);
  2547. if (window->wl.waiting_for_swap_to_commit) {
  2548. callback(id);
  2549. window->wl.frameCallbackData.id = 0;
  2550. window->wl.frameCallbackData.callback = NULL;
  2551. window->wl.frameCallbackData.current_wl_callback = NULL;
  2552. } else {
  2553. window->wl.frameCallbackData.id = id;
  2554. window->wl.frameCallbackData.callback = callback;
  2555. window->wl.frameCallbackData.current_wl_callback = wl_surface_frame(window->wl.surface);
  2556. if (window->wl.frameCallbackData.current_wl_callback) {
  2557. wl_callback_add_listener(window->wl.frameCallbackData.current_wl_callback, &frame_listener, window);
  2558. commit_window_surface_if_safe(window);
  2559. }
  2560. }
  2561. }
  2562. GLFWAPI unsigned long long glfwDBusUserNotify(const GLFWDBUSNotificationData *n, GLFWDBusnotificationcreatedfun callback, void *data) {
  2563. return glfw_dbus_send_user_notification(n, callback, data);
  2564. }
  2565. GLFWAPI void glfwDBusSetUserNotificationHandler(GLFWDBusnotificationactivatedfun handler) {
  2566. glfw_dbus_set_user_notification_activated_handler(handler);
  2567. }
  2568. GLFWAPI bool glfwWaylandSetTitlebarColor(GLFWwindow *handle, uint32_t color, bool use_system_color) {
  2569. _GLFWwindow* window = (_GLFWwindow*) handle;
  2570. if (!window->wl.decorations.serverSide) {
  2571. csd_set_titlebar_color(window, color, use_system_color);
  2572. return true;
  2573. }
  2574. return false;
  2575. }
  2576. GLFWAPI void glfwWaylandRedrawCSDWindowTitle(GLFWwindow *handle) {
  2577. _GLFWwindow* window = (_GLFWwindow*) handle;
  2578. if (csd_change_title(window)) commit_window_surface_if_safe(window);
  2579. }
  2580. const GLFWLayerShellConfig*
  2581. _glfwPlatformGetLayerShellConfig(_GLFWwindow *window) {
  2582. return &window->wl.layer_shell.config;
  2583. }
  2584. GLFWAPI bool glfwIsLayerShellSupported(void) { return _glfw.wl.zwlr_layer_shell_v1 != NULL; }
  2585. GLFWAPI bool glfwWaylandIsWindowFullyCreated(GLFWwindow *handle) { return handle != NULL && ((_GLFWwindow*)handle)->wl.window_fully_created; }
  2586. void
  2587. _glfwPlatformInputColorScheme(GLFWColorScheme appearance UNUSED) {
  2588. _GLFWwindow* window = _glfw.windowListHead;
  2589. while (window) {
  2590. glfwWaylandRedrawCSDWindowTitle((GLFWwindow*)window);
  2591. window = window->next;
  2592. }
  2593. }
  2594. GLFWAPI bool glfwWaylandBeep(GLFWwindow *handle) {
  2595. if (!_glfw.wl.xdg_system_bell_v1) return false;
  2596. _GLFWwindow *window = (_GLFWwindow*)handle;
  2597. xdg_system_bell_v1_ring(_glfw.wl.xdg_system_bell_v1, window ? window->wl.surface : NULL);
  2598. return true;
  2599. }