wl_window.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780
  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 GLFWLayerShellConfig layer_shell_config_for_next_window = {0};
  42. static bool
  43. is_layer_shell(_GLFWwindow *window) { return window->wl.layer_shell.config.type != GLFW_LAYER_SHELL_NONE; }
  44. static void
  45. activation_token_done(void *data, struct xdg_activation_token_v1 *xdg_token, const char *token) {
  46. for (size_t i = 0; i < _glfw.wl.activation_requests.sz; i++) {
  47. glfw_wl_xdg_activation_request *r = _glfw.wl.activation_requests.array + i;
  48. if (r->request_id == (uintptr_t)data) {
  49. _GLFWwindow *window = _glfwWindowForId(r->window_id);
  50. if (r->callback) r->callback((GLFWwindow*)window, token, r->callback_data);
  51. remove_i_from_array(_glfw.wl.activation_requests.array, i, _glfw.wl.activation_requests.sz);
  52. break;
  53. }
  54. }
  55. xdg_activation_token_v1_destroy(xdg_token);
  56. }
  57. static const struct
  58. xdg_activation_token_v1_listener activation_token_listener = {
  59. .done = &activation_token_done,
  60. };
  61. static bool
  62. get_activation_token(
  63. _GLFWwindow *window, uint32_t serial, GLFWactivationcallback cb, void *cb_data
  64. ) {
  65. #define fail(msg) { _glfwInputError(GLFW_PLATFORM_ERROR, msg); if (cb) cb((GLFWwindow*)window, NULL, cb_data); return false; }
  66. if (_glfw.wl.xdg_activation_v1 == NULL) fail("Wayland: activation requests not supported by this Wayland compositor");
  67. struct xdg_activation_token_v1 *token = xdg_activation_v1_get_activation_token(_glfw.wl.xdg_activation_v1);
  68. if (token == NULL) fail("Wayland: failed to create activation request token");
  69. if (_glfw.wl.activation_requests.capacity < _glfw.wl.activation_requests.sz + 1) {
  70. _glfw.wl.activation_requests.capacity = MAX(64u, _glfw.wl.activation_requests.capacity * 2);
  71. _glfw.wl.activation_requests.array = realloc(_glfw.wl.activation_requests.array, _glfw.wl.activation_requests.capacity * sizeof(_glfw.wl.activation_requests.array[0]));
  72. if (!_glfw.wl.activation_requests.array) {
  73. _glfw.wl.activation_requests.capacity = 0;
  74. fail("Wayland: Out of memory while allocation activation request");
  75. }
  76. }
  77. glfw_wl_xdg_activation_request *r = _glfw.wl.activation_requests.array + _glfw.wl.activation_requests.sz++;
  78. memset(r, 0, sizeof(*r));
  79. static uintptr_t rq = 0;
  80. r->window_id = window->id;
  81. r->callback = cb; r->callback_data = cb_data;
  82. r->request_id = ++rq; r->token = token;
  83. if (serial != 0)
  84. xdg_activation_token_v1_set_serial(token, serial, _glfw.wl.seat);
  85. xdg_activation_token_v1_set_surface(token, window->wl.surface);
  86. xdg_activation_token_v1_add_listener(token, &activation_token_listener, (void*)r->request_id);
  87. xdg_activation_token_v1_commit(token);
  88. return true;
  89. #undef fail
  90. }
  91. static struct wl_buffer* createShmBuffer(const GLFWimage* image, bool is_opaque, bool init_data)
  92. {
  93. struct wl_shm_pool* pool;
  94. struct wl_buffer* buffer;
  95. int stride = image->width * 4;
  96. int length = image->width * image->height * 4;
  97. void* data;
  98. int fd, i;
  99. fd = createAnonymousFile(length);
  100. if (fd < 0)
  101. {
  102. _glfwInputError(GLFW_PLATFORM_ERROR,
  103. "Wayland: Creating a buffer file for %d B failed: %s",
  104. length, strerror(errno));
  105. return NULL;
  106. }
  107. data = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  108. if (data == MAP_FAILED)
  109. {
  110. _glfwInputError(GLFW_PLATFORM_ERROR,
  111. "Wayland: mmap failed: %s", strerror(errno));
  112. close(fd);
  113. return NULL;
  114. }
  115. pool = wl_shm_create_pool(_glfw.wl.shm, fd, length);
  116. close(fd);
  117. if (init_data) {
  118. unsigned char* source = (unsigned char*) image->pixels;
  119. unsigned char* target = data;
  120. for (i = 0; i < image->width * image->height; i++, source += 4)
  121. {
  122. unsigned int alpha = source[3];
  123. *target++ = (unsigned char) ((source[2] * alpha) / 255);
  124. *target++ = (unsigned char) ((source[1] * alpha) / 255);
  125. *target++ = (unsigned char) ((source[0] * alpha) / 255);
  126. *target++ = (unsigned char) alpha;
  127. }
  128. }
  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. #undef C
  600. }
  601. }
  602. debug("\n");
  603. if (new_states & TOPLEVEL_STATE_RESIZING) {
  604. if (width) window->wl.user_requested_content_size.width = width;
  605. if (height) window->wl.user_requested_content_size.height = height;
  606. if (!(window->wl.current.toplevel_states & TOPLEVEL_STATE_RESIZING)) report_live_resize(window, true);
  607. }
  608. if (width != 0 && height != 0)
  609. {
  610. if (!(new_states & TOPLEVEL_STATE_DOCKED))
  611. {
  612. if (window->numer != GLFW_DONT_CARE && window->denom != GLFW_DONT_CARE)
  613. {
  614. aspectRatio = (float)width / (float)height;
  615. targetRatio = (float)window->numer / (float)window->denom;
  616. if (aspectRatio < targetRatio)
  617. height = (int32_t)((float)width / targetRatio);
  618. else if (aspectRatio > targetRatio)
  619. width = (int32_t)((float)height * targetRatio);
  620. }
  621. }
  622. }
  623. window->wl.pending.toplevel_states = new_states;
  624. window->wl.pending.width = width;
  625. window->wl.pending.height = height;
  626. window->wl.pending_state |= PENDING_STATE_TOPLEVEL;
  627. }
  628. static void xdgToplevelHandleClose(void* data,
  629. struct xdg_toplevel* toplevel UNUSED)
  630. {
  631. _GLFWwindow* window = data;
  632. window->wl.window_fully_created = true;
  633. _glfwInputWindowCloseRequest(window);
  634. }
  635. #if defined(XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION)
  636. static void
  637. xdg_toplevel_wm_capabilities(void *data, struct xdg_toplevel *xdg_toplevel UNUSED, struct wl_array *caps) {
  638. _GLFWwindow *window = data;
  639. #define c (window->wl.wm_capabilities)
  640. memset(&c, 0, sizeof(c));
  641. enum xdg_toplevel_wm_capabilities *cap;
  642. wl_array_for_each(cap, caps) {
  643. switch (*cap) {
  644. case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE: c.maximize = true; break;
  645. case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE: c.minimize = true; break;
  646. case XDG_TOPLEVEL_WM_CAPABILITIES_WINDOW_MENU: c.window_menu = true; break;
  647. case XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN: c.fullscreen = true; break;
  648. }
  649. }
  650. debug("Compositor top-level capabilities: maximize=%d minimize=%d window_menu=%d fullscreen=%d\n",
  651. c.maximize, c.minimize, c.window_menu, c.fullscreen);
  652. #undef c
  653. }
  654. #endif
  655. static void
  656. xdg_toplevel_configure_bounds(void *data, struct xdg_toplevel *xdg_toplevel UNUSED, int32_t width, int32_t height) {
  657. _GLFWwindow *window = data;
  658. window->wl.xdg.top_level_bounds.width = width;
  659. window->wl.xdg.top_level_bounds.height = height;
  660. debug("Compositor set top-level bounds of: %dx%d for window %llu\n", width, height, window->id);
  661. }
  662. static const struct xdg_toplevel_listener xdgToplevelListener = {
  663. .configure = xdgToplevelHandleConfigure,
  664. .close = xdgToplevelHandleClose,
  665. #ifdef XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION
  666. .configure_bounds = xdg_toplevel_configure_bounds,
  667. .wm_capabilities = xdg_toplevel_wm_capabilities,
  668. #endif
  669. };
  670. static void
  671. update_fully_created_on_configure(_GLFWwindow *window) {
  672. // See fractional_scale_preferred_scale() for logic
  673. if (!window->wl.window_fully_created) {
  674. window->wl.window_fully_created = window->wl.once.fractional_scale_received;
  675. if (window->wl.window_fully_created) debug("Marked window as fully created in configure event\n");
  676. }
  677. }
  678. static void
  679. apply_xdg_configure_changes(_GLFWwindow *window) {
  680. bool suspended_changed = false;
  681. if (window->wl.pending_state & PENDING_STATE_TOPLEVEL) {
  682. uint32_t new_states = window->wl.pending.toplevel_states;
  683. int width = window->wl.pending.width;
  684. int height = window->wl.pending.height;
  685. if (!window->wl.once.surface_configured) {
  686. window->swaps_disallowed = false;
  687. wait_for_swap_to_commit(window);
  688. window->wl.once.surface_configured = true;
  689. update_fully_created_on_configure(window);
  690. }
  691. #ifdef XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION
  692. suspended_changed = ((new_states & TOPLEVEL_STATE_SUSPENDED) != (window->wl.current.toplevel_states & TOPLEVEL_STATE_SUSPENDED));
  693. #endif
  694. if (new_states != window->wl.current.toplevel_states ||
  695. width != window->wl.current.width ||
  696. height != window->wl.current.height) {
  697. bool live_resize_done = !(new_states & TOPLEVEL_STATE_RESIZING) && (window->wl.current.toplevel_states & TOPLEVEL_STATE_RESIZING);
  698. window->wl.current.toplevel_states = new_states;
  699. window->wl.current.width = width;
  700. window->wl.current.height = height;
  701. _glfwInputWindowFocus(window, window->wl.current.toplevel_states & TOPLEVEL_STATE_ACTIVATED);
  702. if (live_resize_done) report_live_resize(window, false);
  703. }
  704. }
  705. if (window->wl.pending_state & PENDING_STATE_DECORATION) {
  706. uint32_t mode = window->wl.pending.decoration_mode;
  707. bool has_server_side_decorations = (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
  708. window->wl.decorations.serverSide = has_server_side_decorations;
  709. window->wl.current.decoration_mode = mode;
  710. }
  711. if (window->wl.pending_state) {
  712. int width = window->wl.pending.width, height = window->wl.pending.height;
  713. csd_set_window_geometry(window, &width, &height);
  714. bool resized = dispatchChangesAfterConfigure(window, width, height);
  715. csd_set_visible(window, !(window->wl.decorations.serverSide || window->monitor || window->wl.current.toplevel_states & TOPLEVEL_STATE_FULLSCREEN));
  716. debug("Final window %llu content size: %dx%d resized: %d\n", window->id, width, height, resized);
  717. }
  718. inform_compositor_of_window_geometry(window, "configure");
  719. commit_window_surface_if_safe(window);
  720. window->wl.pending_state = 0;
  721. #ifdef XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION
  722. if (suspended_changed) {
  723. _glfwInputWindowOcclusion(window, window->wl.current.toplevel_states & TOPLEVEL_STATE_SUSPENDED);
  724. }
  725. #endif
  726. }
  727. typedef union pixel {
  728. struct {
  729. uint8_t blue, green, red, alpha;
  730. };
  731. uint32_t value;
  732. } pixel;
  733. static struct wl_buffer*
  734. create_single_color_buffer(int width, int height, pixel color) {
  735. // convert to pre-multiplied alpha as that's what wayland wants
  736. if (width == 1 && height == 1 && _glfw.wl.wp_single_pixel_buffer_manager_v1) {
  737. #define C(x) (uint32_t)(((double)((uint64_t)color.alpha * color.x * UINT32_MAX)) / (255 * 255))
  738. struct wl_buffer *ans = wp_single_pixel_buffer_manager_v1_create_u32_rgba_buffer(
  739. _glfw.wl.wp_single_pixel_buffer_manager_v1, C(red), C(green), C(blue), (uint32_t)((color.alpha / 255.) * UINT32_MAX));
  740. #undef C
  741. if (!ans) _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to create single pixel buffer");
  742. return ans;
  743. }
  744. float alpha = color.alpha / 255.f;
  745. color.red = (uint8_t)(alpha * color.red); color.green = (uint8_t)(alpha * color.green); color.blue = (uint8_t)(alpha * color.blue);
  746. int shm_format = color.alpha == 0xff ? WL_SHM_FORMAT_XRGB8888 : WL_SHM_FORMAT_ARGB8888;
  747. const size_t size = 4 * width * height;
  748. int fd = createAnonymousFile(size);
  749. if (fd < 0) {
  750. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to create anonymous file");
  751. return NULL;
  752. }
  753. uint32_t *shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  754. if (color.value) for (size_t i = 0; i < size/4; i++) shm_data[i] = color.value;
  755. else memset(shm_data, 0, size);
  756. if (!shm_data) {
  757. close(fd);
  758. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to mmap anonymous file");
  759. return NULL;
  760. }
  761. struct wl_shm_pool *pool = wl_shm_create_pool(_glfw.wl.shm, fd, size);
  762. if (!pool) {
  763. close(fd); munmap(shm_data, size);
  764. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to create wl_shm_pool of size: %zu", size);
  765. return NULL;
  766. }
  767. struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, width, height, width * 4, shm_format);
  768. wl_shm_pool_destroy(pool); munmap(shm_data, size); close(fd);
  769. if (!buffer) {
  770. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: failed to create wl_buffer of size: %zu", size);
  771. return NULL;
  772. }
  773. return buffer;
  774. }
  775. static bool
  776. attach_temp_buffer_during_window_creation(_GLFWwindow *window) {
  777. pixel color;
  778. color.value = _glfw.hints.window.wl.bgcolor;
  779. if (!window->wl.transparent) color.alpha = 0xff;
  780. else if (color.alpha == 0) color.value = 0; // fully transparent blends best with black and we can use memset
  781. if (window->wl.temp_buffer_used_during_window_creation) {
  782. wl_buffer_destroy(window->wl.temp_buffer_used_during_window_creation);
  783. window->wl.temp_buffer_used_during_window_creation = NULL;
  784. }
  785. int width, height;
  786. _glfwPlatformGetFramebufferSize(window, &width, &height);
  787. if (window->wl.wp_viewport) {
  788. window->wl.temp_buffer_used_during_window_creation = create_single_color_buffer(1, 1, color);
  789. wl_surface_set_buffer_scale(window->wl.surface, 1);
  790. wp_viewport_set_destination(window->wl.wp_viewport, window->wl.width, window->wl.height);
  791. } else {
  792. window->wl.temp_buffer_used_during_window_creation = create_single_color_buffer(width, height, color);
  793. wl_surface_set_buffer_scale(window->wl.surface, window->wl.fractional_scale ? 1: _glfwWaylandIntegerWindowScale(window));
  794. }
  795. if (!window->wl.temp_buffer_used_during_window_creation) return false;
  796. wl_surface_attach(window->wl.surface, window->wl.temp_buffer_used_during_window_creation, 0, 0);
  797. 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);
  798. commit_window_surface(window);
  799. return true;
  800. }
  801. static void
  802. loop_till_window_fully_created(_GLFWwindow *window) {
  803. if (!window->wl.window_fully_created) {
  804. GLFWwindow *ctx = glfwGetCurrentContext();
  805. debug("Waiting for compositor to send fractional scale for window %llu\n", window->id);
  806. monotonic_t start = monotonic();
  807. while (!window->wl.window_fully_created && monotonic() - start < ms_to_monotonic_t(300)) {
  808. if (wl_display_roundtrip(_glfw.wl.display) == -1) {
  809. window->wl.window_fully_created = true;
  810. }
  811. }
  812. window->wl.window_fully_created = true;
  813. // If other OS windows were resized when this window is shown, the ctx might have been changed by
  814. // user code, restore it to whatever it was at the start.
  815. if (glfwGetCurrentContext() != ctx) glfwMakeContextCurrent(ctx);
  816. }
  817. }
  818. static void
  819. xdgSurfaceHandleConfigure(void* data, struct xdg_surface* surface, uint32_t serial) {
  820. // The poorly documented pattern Wayland requires is:
  821. // 1) ack the configure,
  822. // 2) set the window geometry
  823. // 3) attach a new buffer of the correct size to the surface
  824. // 4) only then commit the surface.
  825. // buffer is attached only by eglSwapBuffers,
  826. // so we set a flag to not commit the surface till the next swapbuffers. Note that
  827. // wl_egl_window_resize() does not actually resize the buffer until the next draw call
  828. // or buffer state query.
  829. _GLFWwindow* window = data;
  830. xdg_surface_ack_configure(surface, serial);
  831. debug("XDG surface configure event received and acknowledged for window %llu\n", window->id);
  832. apply_xdg_configure_changes(window);
  833. if (!window->wl.window_fully_created) {
  834. if (!attach_temp_buffer_during_window_creation(window)) window->wl.window_fully_created = true;
  835. }
  836. }
  837. static const struct xdg_surface_listener xdgSurfaceListener = {
  838. xdgSurfaceHandleConfigure
  839. };
  840. static void
  841. setXdgDecorations(_GLFWwindow* window)
  842. {
  843. if (window->wl.xdg.decoration) {
  844. window->wl.decorations.serverSide = true;
  845. 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);
  846. } else {
  847. window->wl.decorations.serverSide = false;
  848. csd_set_visible(window, window->decorated);
  849. }
  850. }
  851. void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, bool enabled UNUSED) {
  852. setXdgDecorations(window);
  853. inform_compositor_of_window_geometry(window, "SetWindowDecorated");
  854. commit_window_surface_if_safe(window);
  855. }
  856. static struct wl_output*
  857. find_output_by_name(const char* name) {
  858. if (!name || !name[0]) return NULL;
  859. for (int i = 0; i < _glfw.monitorCount; i++) {
  860. _GLFWmonitor *m = _glfw.monitors[i];
  861. if (strcmp(m->wl.friendly_name, name) == 0) return m->wl.output;
  862. }
  863. return NULL;
  864. }
  865. static void
  866. layer_set_properties(_GLFWwindow *window) {
  867. 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;
  868. int exclusive_zone = window->wl.layer_shell.config.requested_exclusive_zone;
  869. enum zwlr_layer_surface_v1_keyboard_interactivity focus_policy = ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE;
  870. switch(window->wl.layer_shell.config.focus_policy) {
  871. case GLFW_FOCUS_NOT_ALLOWED: focus_policy = ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE; break;
  872. case GLFW_FOCUS_EXCLUSIVE: focus_policy = ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE; break;
  873. case GLFW_FOCUS_ON_DEMAND: focus_policy = ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_ON_DEMAND; break;
  874. }
  875. int panel_width = 0, panel_height = 0;
  876. switch (window->wl.layer_shell.config.type) {
  877. case GLFW_LAYER_SHELL_NONE: break;
  878. case GLFW_LAYER_SHELL_BACKGROUND: exclusive_zone = -1; break;
  879. case GLFW_LAYER_SHELL_TOP:
  880. case GLFW_LAYER_SHELL_OVERLAY:
  881. case GLFW_LAYER_SHELL_PANEL:
  882. switch (window->wl.layer_shell.config.edge) {
  883. case GLFW_EDGE_TOP:
  884. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
  885. panel_height = window->wl.height;
  886. if (!window->wl.layer_shell.config.override_exclusive_zone) exclusive_zone = window->wl.height;
  887. break;
  888. case GLFW_EDGE_BOTTOM:
  889. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
  890. panel_height = window->wl.height;
  891. if (!window->wl.layer_shell.config.override_exclusive_zone) exclusive_zone = window->wl.height;
  892. break;
  893. case GLFW_EDGE_LEFT:
  894. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
  895. panel_width = window->wl.width;
  896. if (!window->wl.layer_shell.config.override_exclusive_zone) exclusive_zone = window->wl.width;
  897. break;
  898. case GLFW_EDGE_RIGHT:
  899. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
  900. panel_width = window->wl.width;
  901. if (!window->wl.layer_shell.config.override_exclusive_zone) exclusive_zone = window->wl.width;
  902. break;
  903. case GLFW_EDGE_NONE:
  904. which_anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; // None is anchored to "top left"
  905. panel_width = window->wl.width;
  906. panel_height = window->wl.height;
  907. break;
  908. }
  909. }
  910. #define surface window->wl.layer_shell.zwlr_layer_surface_v1
  911. zwlr_layer_surface_v1_set_size(surface, panel_width, panel_height);
  912. if (window->wl.wp_viewport) wp_viewport_set_destination(window->wl.wp_viewport, window->wl.width, window->wl.height);
  913. debug("Compositor will be informed that layer size: %dx%d viewport: %dx%d at next surface commit\n", panel_width, panel_height, window->wl.width, window->wl.height);
  914. zwlr_layer_surface_v1_set_anchor(surface, which_anchor);
  915. zwlr_layer_surface_v1_set_exclusive_zone(surface, exclusive_zone);
  916. zwlr_layer_surface_v1_set_margin(surface, window->wl.layer_shell.config.requested_top_margin, window->wl.layer_shell.config.requested_right_margin, window->wl.layer_shell.config.requested_bottom_margin, window->wl.layer_shell.config.requested_left_margin);
  917. zwlr_layer_surface_v1_set_keyboard_interactivity(surface, focus_policy);
  918. #undef surface
  919. }
  920. static void
  921. layer_surface_handle_configure(void* data, struct zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t width, uint32_t height) {
  922. debug("Layer shell configure event: width: %u height: %u\n", width, height);
  923. _GLFWwindow* window = data;
  924. if (!window->wl.once.surface_configured) {
  925. window->swaps_disallowed = false;
  926. wait_for_swap_to_commit(window);
  927. window->wl.once.surface_configured = true;
  928. update_fully_created_on_configure(window);
  929. }
  930. GLFWvidmode m = {0};
  931. if (window->wl.monitorsCount) _glfwPlatformGetVideoMode(window->wl.monitors[0], &m);
  932. window->wl.layer_shell.config.size_callback(
  933. (GLFWwindow*)window, &window->wl.layer_shell.config, m.width, m.height, &width, &height);
  934. zwlr_layer_surface_v1_ack_configure(surface, serial);
  935. if ((int)width != window->wl.width || (int)height != window->wl.height) {
  936. debug("Layer shell size changed to %ux%u in layer_surface_handle_configure\n", width, height);
  937. _glfwInputWindowSize(window, width, height);
  938. window->wl.width = width; window->wl.height = height;
  939. resizeFramebuffer(window);
  940. _glfwInputWindowDamage(window);
  941. layer_set_properties(window);
  942. }
  943. commit_window_surface_if_safe(window);
  944. if (!window->wl.window_fully_created) {
  945. if (!attach_temp_buffer_during_window_creation(window)) window->wl.window_fully_created = true;
  946. }
  947. }
  948. static void
  949. layer_surface_handle_close_requested(void* data, struct zwlr_layer_surface_v1* surface UNUSED) {
  950. _GLFWwindow* window = data;
  951. window->wl.window_fully_created = true;
  952. _glfwInputWindowCloseRequest(window);
  953. }
  954. static const struct zwlr_layer_surface_v1_listener zwlr_layer_surface_v1_listener = {
  955. .configure=layer_surface_handle_configure,
  956. .closed=layer_surface_handle_close_requested,
  957. };
  958. static bool
  959. create_layer_shell_surface(_GLFWwindow *window) {
  960. if (!_glfw.wl.zwlr_layer_shell_v1) {
  961. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: wlr-layer-shell protocol unsupported by compositor");
  962. return false;
  963. }
  964. window->decorated = false; // shell windows must not have decorations
  965. struct wl_output *wl_output = find_output_by_name(window->wl.layer_shell.config.output_name);
  966. enum zwlr_layer_shell_v1_layer which_layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND; // Default to background
  967. switch (window->wl.layer_shell.config.type) {
  968. case GLFW_LAYER_SHELL_BACKGROUND: break; case GLFW_LAYER_SHELL_NONE: break;
  969. case GLFW_LAYER_SHELL_PANEL: which_layer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM; break;
  970. case GLFW_LAYER_SHELL_TOP: which_layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP; break;
  971. case GLFW_LAYER_SHELL_OVERLAY: which_layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY; break;
  972. }
  973. #define ls window->wl.layer_shell.zwlr_layer_surface_v1
  974. ls = zwlr_layer_shell_v1_get_layer_surface(
  975. _glfw.wl.zwlr_layer_shell_v1, window->wl.surface, wl_output, which_layer, "kitty");
  976. if (!ls) {
  977. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: layer-surface creation failed");
  978. return false;
  979. }
  980. zwlr_layer_surface_v1_add_listener(ls, &zwlr_layer_surface_v1_listener, window);
  981. layer_set_properties(window);
  982. commit_window_surface(window);
  983. wl_display_roundtrip(_glfw.wl.display);
  984. #undef ls
  985. return true;
  986. }
  987. static bool
  988. create_window_desktop_surface(_GLFWwindow* window)
  989. {
  990. if (is_layer_shell(window)) return create_layer_shell_surface(window);
  991. window->wl.xdg.surface = xdg_wm_base_get_xdg_surface(_glfw.wl.wmBase,
  992. window->wl.surface);
  993. if (!window->wl.xdg.surface)
  994. {
  995. _glfwInputError(GLFW_PLATFORM_ERROR,
  996. "Wayland: xdg-surface creation failed");
  997. return false;
  998. }
  999. xdg_surface_add_listener(window->wl.xdg.surface,
  1000. &xdgSurfaceListener,
  1001. window);
  1002. window->wl.xdg.toplevel = xdg_surface_get_toplevel(window->wl.xdg.surface);
  1003. if (!window->wl.xdg.toplevel)
  1004. {
  1005. _glfwInputError(GLFW_PLATFORM_ERROR,
  1006. "Wayland: xdg-toplevel creation failed");
  1007. return false;
  1008. }
  1009. #ifdef XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION
  1010. if (_glfw.wl.xdg_wm_base_version < XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION) {
  1011. window->wl.wm_capabilities.maximize = true; window->wl.wm_capabilities.minimize = true; window->wl.wm_capabilities.fullscreen = true;
  1012. window->wl.wm_capabilities.window_menu = true;
  1013. }
  1014. #endif
  1015. xdg_toplevel_add_listener(window->wl.xdg.toplevel, &xdgToplevelListener, window);
  1016. if (_glfw.wl.decorationManager) {
  1017. window->wl.xdg.decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(
  1018. _glfw.wl.decorationManager, window->wl.xdg.toplevel);
  1019. zxdg_toplevel_decoration_v1_add_listener(window->wl.xdg.decoration, &xdgDecorationListener, window);
  1020. }
  1021. if (strlen(window->wl.appId))
  1022. xdg_toplevel_set_app_id(window->wl.xdg.toplevel, window->wl.appId);
  1023. if (window->wl.title)
  1024. xdg_toplevel_set_title(window->wl.xdg.toplevel, window->wl.title);
  1025. if (window->minwidth != GLFW_DONT_CARE && window->minheight != GLFW_DONT_CARE)
  1026. xdg_toplevel_set_min_size(window->wl.xdg.toplevel,
  1027. window->minwidth, window->minheight);
  1028. if (window->maxwidth != GLFW_DONT_CARE && window->maxheight != GLFW_DONT_CARE)
  1029. xdg_toplevel_set_max_size(window->wl.xdg.toplevel,
  1030. window->maxwidth, window->maxheight);
  1031. if (window->monitor) {
  1032. if (window->wl.wm_capabilities.fullscreen)
  1033. xdg_toplevel_set_fullscreen(window->wl.xdg.toplevel, window->monitor->wl.output);
  1034. else
  1035. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland compositor does not support fullscreen");
  1036. } else {
  1037. if (window->wl.maximize_on_first_show) {
  1038. window->wl.maximize_on_first_show = false;
  1039. xdg_toplevel_set_maximized(window->wl.xdg.toplevel);
  1040. }
  1041. setXdgDecorations(window);
  1042. }
  1043. commit_window_surface(window);
  1044. wl_display_roundtrip(_glfw.wl.display);
  1045. return true;
  1046. }
  1047. static void incrementCursorImage(_GLFWwindow* window)
  1048. {
  1049. if (window && window->wl.decorations.focus == CENTRAL_WINDOW && window->cursorMode != GLFW_CURSOR_HIDDEN) {
  1050. _GLFWcursor* cursor = window->wl.currentCursor;
  1051. if (cursor && cursor->wl.cursor && cursor->wl.cursor->image_count)
  1052. {
  1053. cursor->wl.currentImage += 1;
  1054. cursor->wl.currentImage %= cursor->wl.cursor->image_count;
  1055. setCursorImage(window, false);
  1056. toggleTimer(&_glfw.wl.eventLoopData, _glfw.wl.cursorAnimationTimer, cursor->wl.cursor->image_count > 1);
  1057. return;
  1058. }
  1059. }
  1060. toggleTimer(&_glfw.wl.eventLoopData, _glfw.wl.cursorAnimationTimer, 1);
  1061. }
  1062. void
  1063. animateCursorImage(id_type timer_id UNUSED, void *data UNUSED) {
  1064. incrementCursorImage(_glfw.wl.pointerFocus);
  1065. }
  1066. static void
  1067. abortOnFatalError(int last_error) {
  1068. static bool abort_called = false;
  1069. if (!abort_called) {
  1070. abort_called = true;
  1071. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: fatal display error: %s", strerror(last_error));
  1072. if (_glfw.callbacks.application_close) _glfw.callbacks.application_close(1);
  1073. else {
  1074. _GLFWwindow* window = _glfw.windowListHead;
  1075. while (window)
  1076. {
  1077. _glfwInputWindowCloseRequest(window);
  1078. window = window->next;
  1079. }
  1080. }
  1081. }
  1082. // ensure the tick callback is called
  1083. _glfw.wl.eventLoopData.wakeup_data_read = true;
  1084. }
  1085. static void
  1086. wayland_read_events(int poll_result, int events, void *data UNUSED) {
  1087. EVDBG("wayland_read_events poll_result: %d events: %d", poll_result, events);
  1088. if (poll_result > 0 && events) wl_display_read_events(_glfw.wl.display);
  1089. else wl_display_cancel_read(_glfw.wl.display);
  1090. }
  1091. static void handleEvents(monotonic_t timeout)
  1092. {
  1093. struct wl_display* display = _glfw.wl.display;
  1094. errno = 0;
  1095. EVDBG("starting handleEvents(%.2f)", monotonic_t_to_s_double(timeout));
  1096. while (wl_display_prepare_read(display) != 0) {
  1097. if (wl_display_dispatch_pending(display) == -1) {
  1098. abortOnFatalError(errno);
  1099. return;
  1100. }
  1101. }
  1102. // If an error different from EAGAIN happens, we have likely been
  1103. // disconnected from the Wayland session, try to handle that the best we
  1104. // can.
  1105. errno = 0;
  1106. if (wl_display_flush(display) < 0 && errno != EAGAIN)
  1107. {
  1108. wl_display_cancel_read(display);
  1109. abortOnFatalError(errno);
  1110. return;
  1111. }
  1112. // we pass in wayland_read_events to ensure that the above wl_display_prepare_read call
  1113. // is followed by either wl_display_cancel_read or wl_display_read_events
  1114. // before any events/timers are dispatched. This allows other wayland functions
  1115. // to be called in the event/timer handlers without causing a deadlock
  1116. bool display_read_ok = pollForEvents(&_glfw.wl.eventLoopData, timeout, wayland_read_events);
  1117. EVDBG("display_read_ok: %d", display_read_ok);
  1118. if (display_read_ok) {
  1119. int num = wl_display_dispatch_pending(display);
  1120. (void)num;
  1121. EVDBG("dispatched %d Wayland events", num);
  1122. }
  1123. glfw_ibus_dispatch(&_glfw.wl.xkb.ibus);
  1124. glfw_dbus_session_bus_dispatch();
  1125. EVDBG("other dispatch done");
  1126. if (_glfw.wl.eventLoopData.wakeup_fd_ready) check_for_wakeup_events(&_glfw.wl.eventLoopData);
  1127. }
  1128. static struct wl_cursor*
  1129. try_cursor_names(struct wl_cursor_theme* theme, int arg_count, ...) {
  1130. struct wl_cursor* ans = NULL;
  1131. va_list ap;
  1132. va_start(ap, arg_count);
  1133. for (int i = 0; i < arg_count && !ans; i++) {
  1134. const char *name = va_arg(ap, const char *);
  1135. ans = wl_cursor_theme_get_cursor(theme, name);
  1136. }
  1137. va_end(ap);
  1138. return ans;
  1139. }
  1140. struct wl_cursor* _glfwLoadCursor(GLFWCursorShape shape, struct wl_cursor_theme* theme)
  1141. {
  1142. static bool warnings[GLFW_INVALID_CURSOR] = {0};
  1143. if (!theme) return NULL;
  1144. #define NUMARGS(...) (sizeof((const char*[]){__VA_ARGS__})/sizeof(const char*))
  1145. #define C(name, ...) case name: { \
  1146. ans = try_cursor_names(theme, NUMARGS(__VA_ARGS__), __VA_ARGS__); \
  1147. if (!ans && !warnings[name]) {\
  1148. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: Could not find standard cursor: %s", #name); \
  1149. warnings[name] = true; \
  1150. } \
  1151. break; }
  1152. struct wl_cursor* ans = NULL;
  1153. switch (shape)
  1154. {
  1155. /* start glfw to xc mapping (auto generated by gen-key-constants.py do not edit) */
  1156. C(GLFW_DEFAULT_CURSOR, "default", "left_ptr");
  1157. C(GLFW_TEXT_CURSOR, "text", "xterm", "ibeam");
  1158. C(GLFW_POINTER_CURSOR, "pointing_hand", "pointer", "hand2", "hand");
  1159. C(GLFW_HELP_CURSOR, "help", "question_arrow", "whats_this");
  1160. C(GLFW_WAIT_CURSOR, "wait", "clock", "watch");
  1161. C(GLFW_PROGRESS_CURSOR, "progress", "half-busy", "left_ptr_watch");
  1162. C(GLFW_CROSSHAIR_CURSOR, "crosshair", "tcross");
  1163. C(GLFW_CELL_CURSOR, "cell", "plus", "cross");
  1164. C(GLFW_VERTICAL_TEXT_CURSOR, "vertical-text");
  1165. C(GLFW_MOVE_CURSOR, "move", "fleur", "pointer-move");
  1166. C(GLFW_E_RESIZE_CURSOR, "e-resize", "right_side");
  1167. C(GLFW_NE_RESIZE_CURSOR, "ne-resize", "top_right_corner");
  1168. C(GLFW_NW_RESIZE_CURSOR, "nw-resize", "top_left_corner");
  1169. C(GLFW_N_RESIZE_CURSOR, "n-resize", "top_side");
  1170. C(GLFW_SE_RESIZE_CURSOR, "se-resize", "bottom_right_corner");
  1171. C(GLFW_SW_RESIZE_CURSOR, "sw-resize", "bottom_left_corner");
  1172. C(GLFW_S_RESIZE_CURSOR, "s-resize", "bottom_side");
  1173. C(GLFW_W_RESIZE_CURSOR, "w-resize", "left_side");
  1174. C(GLFW_EW_RESIZE_CURSOR, "ew-resize", "sb_h_double_arrow", "split_h");
  1175. C(GLFW_NS_RESIZE_CURSOR, "ns-resize", "sb_v_double_arrow", "split_v");
  1176. C(GLFW_NESW_RESIZE_CURSOR, "nesw-resize", "size_bdiag", "size-bdiag");
  1177. C(GLFW_NWSE_RESIZE_CURSOR, "nwse-resize", "size_fdiag", "size-fdiag");
  1178. C(GLFW_ZOOM_IN_CURSOR, "zoom-in", "zoom_in");
  1179. C(GLFW_ZOOM_OUT_CURSOR, "zoom-out", "zoom_out");
  1180. C(GLFW_ALIAS_CURSOR, "dnd-link");
  1181. C(GLFW_COPY_CURSOR, "dnd-copy");
  1182. C(GLFW_NOT_ALLOWED_CURSOR, "not-allowed", "forbidden", "crossed_circle");
  1183. C(GLFW_NO_DROP_CURSOR, "no-drop", "dnd-no-drop");
  1184. C(GLFW_GRAB_CURSOR, "grab", "openhand", "hand1");
  1185. C(GLFW_GRABBING_CURSOR, "grabbing", "closedhand", "dnd-none");
  1186. /* end glfw to xc mapping */
  1187. case GLFW_INVALID_CURSOR:
  1188. break;
  1189. }
  1190. return ans;
  1191. #undef NUMARGS
  1192. #undef C
  1193. }
  1194. //////////////////////////////////////////////////////////////////////////
  1195. ////// GLFW platform API //////
  1196. //////////////////////////////////////////////////////////////////////////
  1197. int _glfwPlatformCreateWindow(_GLFWwindow* window,
  1198. const _GLFWwndconfig* wndconfig,
  1199. const _GLFWctxconfig* ctxconfig,
  1200. const _GLFWfbconfig* fbconfig)
  1201. {
  1202. window->wl.layer_shell.config = layer_shell_config_for_next_window;
  1203. memset(&layer_shell_config_for_next_window, 0, sizeof(layer_shell_config_for_next_window));
  1204. csd_initialize_metrics(window);
  1205. window->wl.transparent = fbconfig->transparent;
  1206. strncpy(window->wl.appId, wndconfig->wl.appId, sizeof(window->wl.appId));
  1207. window->swaps_disallowed = true;
  1208. if (!createSurface(window, wndconfig)) return false;
  1209. if (wndconfig->title) window->wl.title = _glfw_strdup(wndconfig->title);
  1210. if (wndconfig->maximized) window->wl.maximize_on_first_show = true;
  1211. if (wndconfig->visible)
  1212. {
  1213. if (!create_window_desktop_surface(window))
  1214. return false;
  1215. window->wl.visible = true;
  1216. }
  1217. else
  1218. {
  1219. window->wl.xdg.surface = NULL;
  1220. window->wl.xdg.toplevel = NULL;
  1221. window->wl.layer_shell.zwlr_layer_surface_v1 = NULL;
  1222. window->wl.visible = false;
  1223. }
  1224. window->wl.currentCursor = NULL;
  1225. // Don't set window->wl.cursorTheme to NULL here.
  1226. window->wl.monitors = calloc(1, sizeof(_GLFWmonitor*));
  1227. window->wl.monitorsCount = 0;
  1228. window->wl.monitorsSize = 1;
  1229. // looping till window fully created attaches a single pixel buffer to the window,
  1230. // this cannot be done once a OpenGL context is created for the window. So first loop
  1231. // and only then create the OpenGL context.
  1232. if (window->wl.visible) loop_till_window_fully_created(window);
  1233. debug("Creating OpenGL context and attaching it to window\n");
  1234. if (ctxconfig->client != GLFW_NO_API)
  1235. {
  1236. if (ctxconfig->source == GLFW_EGL_CONTEXT_API ||
  1237. ctxconfig->source == GLFW_NATIVE_CONTEXT_API)
  1238. {
  1239. if (!_glfwInitEGL())
  1240. return false;
  1241. if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig))
  1242. return false;
  1243. }
  1244. else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API)
  1245. {
  1246. if (!_glfwInitOSMesa())
  1247. return false;
  1248. if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig))
  1249. return false;
  1250. }
  1251. }
  1252. return true;
  1253. }
  1254. void _glfwPlatformDestroyWindow(_GLFWwindow* window)
  1255. {
  1256. if (window == _glfw.wl.pointerFocus)
  1257. {
  1258. _glfw.wl.pointerFocus = NULL;
  1259. _glfwInputCursorEnter(window, false);
  1260. }
  1261. if (window->id == _glfw.wl.keyboardFocusId)
  1262. {
  1263. _glfw.wl.keyboardFocusId = 0;
  1264. _glfwInputWindowFocus(window, false);
  1265. }
  1266. if (window->id == _glfw.wl.keyRepeatInfo.keyboardFocusId) {
  1267. _glfw.wl.keyRepeatInfo.keyboardFocusId = 0;
  1268. }
  1269. if (window->wl.temp_buffer_used_during_window_creation)
  1270. wl_buffer_destroy(window->wl.temp_buffer_used_during_window_creation);
  1271. if (window->wl.wp_fractional_scale_v1)
  1272. wp_fractional_scale_v1_destroy(window->wl.wp_fractional_scale_v1);
  1273. if (window->wl.wp_viewport)
  1274. wp_viewport_destroy(window->wl.wp_viewport);
  1275. if (window->wl.org_kde_kwin_blur)
  1276. org_kde_kwin_blur_release(window->wl.org_kde_kwin_blur);
  1277. if (window->context.destroy)
  1278. window->context.destroy(window);
  1279. csd_free_all_resources(window);
  1280. if (window->wl.xdg.decoration)
  1281. zxdg_toplevel_decoration_v1_destroy(window->wl.xdg.decoration);
  1282. if (window->wl.native)
  1283. wl_egl_window_destroy(window->wl.native);
  1284. if (window->wl.xdg.toplevel)
  1285. xdg_toplevel_destroy(window->wl.xdg.toplevel);
  1286. if (window->wl.xdg.surface)
  1287. xdg_surface_destroy(window->wl.xdg.surface);
  1288. if (window->wl.surface)
  1289. wl_surface_destroy(window->wl.surface);
  1290. free(window->wl.title);
  1291. free(window->wl.monitors);
  1292. if (window->wl.frameCallbackData.current_wl_callback)
  1293. wl_callback_destroy(window->wl.frameCallbackData.current_wl_callback);
  1294. }
  1295. void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
  1296. {
  1297. if (window->wl.title) {
  1298. if (title && strcmp(title, window->wl.title) == 0) return;
  1299. free(window->wl.title);
  1300. } else if (!title) return;
  1301. // Wayland cannot handle requests larger than ~8200 bytes. Sending
  1302. // one causes an abort(). Since titles this large are meaningless anyway
  1303. // ensure they do not happen.
  1304. window->wl.title = utf_8_strndup(title, 2048);
  1305. if (window->wl.xdg.toplevel) {
  1306. xdg_toplevel_set_title(window->wl.xdg.toplevel, window->wl.title);
  1307. csd_change_title(window);
  1308. commit_window_surface_if_safe(window);
  1309. }
  1310. }
  1311. void _glfwPlatformSetWindowIcon(_GLFWwindow* window UNUSED,
  1312. int count UNUSED, const GLFWimage* images UNUSED)
  1313. {
  1314. _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
  1315. "Wayland: The platform does not support setting the window icon");
  1316. }
  1317. void _glfwPlatformGetWindowPos(_GLFWwindow* window UNUSED, int* xpos UNUSED, int* ypos UNUSED)
  1318. {
  1319. // A Wayland client is not aware of its position, so just warn and leave it
  1320. // as (0, 0)
  1321. static bool warned_once = false;
  1322. if (!warned_once) {
  1323. _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
  1324. "Wayland: The platform does not provide the window position");
  1325. warned_once = true;
  1326. }
  1327. }
  1328. void _glfwPlatformSetWindowPos(_GLFWwindow* window UNUSED, int xpos UNUSED, int ypos UNUSED)
  1329. {
  1330. // A Wayland client can not set its position, so just warn
  1331. _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
  1332. "Wayland: The platform does not support setting the window position");
  1333. }
  1334. void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
  1335. {
  1336. if (width)
  1337. *width = window->wl.width;
  1338. if (height)
  1339. *height = window->wl.height;
  1340. }
  1341. void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
  1342. {
  1343. if (width != window->wl.width || height != window->wl.height) {
  1344. window->wl.user_requested_content_size.width = width;
  1345. window->wl.user_requested_content_size.height = height;
  1346. int32_t w = 0, h = 0;
  1347. csd_set_window_geometry(window, &w, &h);
  1348. window->wl.width = w; window->wl.height = h;
  1349. resizeFramebuffer(window);
  1350. csd_set_visible(window, true); // resizes the csd iff the window currently has csd
  1351. commit_window_surface_if_safe(window);
  1352. inform_compositor_of_window_geometry(window, "SetWindowSize");
  1353. }
  1354. }
  1355. void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window,
  1356. int minwidth, int minheight,
  1357. int maxwidth, int maxheight)
  1358. {
  1359. if (window->wl.xdg.toplevel)
  1360. {
  1361. if (minwidth == GLFW_DONT_CARE || minheight == GLFW_DONT_CARE)
  1362. minwidth = minheight = 0;
  1363. if (maxwidth == GLFW_DONT_CARE || maxheight == GLFW_DONT_CARE)
  1364. maxwidth = maxheight = 0;
  1365. xdg_toplevel_set_min_size(window->wl.xdg.toplevel, minwidth, minheight);
  1366. xdg_toplevel_set_max_size(window->wl.xdg.toplevel, maxwidth, maxheight);
  1367. commit_window_surface_if_safe(window);
  1368. }
  1369. }
  1370. void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window UNUSED,
  1371. int numer UNUSED, int denom UNUSED)
  1372. {
  1373. // TODO: find out how to trigger a resize.
  1374. // The actual limits are checked in the xdg_toplevel::configure handler.
  1375. _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
  1376. "Wayland: Window aspect ratio not yet implemented");
  1377. }
  1378. void _glfwPlatformSetWindowSizeIncrements(_GLFWwindow* window UNUSED,
  1379. int widthincr UNUSED, int heightincr UNUSED)
  1380. {
  1381. // TODO: find out how to trigger a resize.
  1382. // The actual limits are checked in the xdg_toplevel::configure handler.
  1383. }
  1384. void _glfwPlatformGetFramebufferSize(_GLFWwindow* window,
  1385. int* width, int* height)
  1386. {
  1387. _glfwPlatformGetWindowSize(window, width, height);
  1388. double fscale = _glfwWaylandWindowScale(window);
  1389. if (width)
  1390. *width = (int)round(*width * fscale);
  1391. if (height)
  1392. *height = (int)round(*height * fscale);
  1393. }
  1394. void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
  1395. int* left, int* top,
  1396. int* right, int* bottom)
  1397. {
  1398. if (window->decorated && !window->monitor && !window->wl.decorations.serverSide)
  1399. {
  1400. if (top)
  1401. *top = window->wl.decorations.metrics.top - window->wl.decorations.metrics.visible_titlebar_height;
  1402. if (left)
  1403. *left = window->wl.decorations.metrics.width;
  1404. if (right)
  1405. *right = window->wl.decorations.metrics.width;
  1406. if (bottom)
  1407. *bottom = window->wl.decorations.metrics.width;
  1408. }
  1409. }
  1410. void _glfwPlatformGetWindowContentScale(_GLFWwindow* window,
  1411. float* xscale, float* yscale)
  1412. {
  1413. float fscale = (float)_glfwWaylandWindowScale(window);
  1414. if (xscale)
  1415. *xscale = fscale;
  1416. if (yscale)
  1417. *yscale = fscale;
  1418. }
  1419. monotonic_t _glfwPlatformGetDoubleClickInterval(_GLFWwindow* window UNUSED)
  1420. {
  1421. return ms_to_monotonic_t(500ll);
  1422. }
  1423. void _glfwPlatformIconifyWindow(_GLFWwindow* window)
  1424. {
  1425. if (window->wl.xdg.toplevel) {
  1426. if (window->wl.wm_capabilities.minimize) xdg_toplevel_set_minimized(window->wl.xdg.toplevel);
  1427. else _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland compositor does not support minimizing windows");
  1428. }
  1429. }
  1430. void _glfwPlatformRestoreWindow(_GLFWwindow* window)
  1431. {
  1432. if (window->wl.xdg.toplevel)
  1433. {
  1434. if (window->monitor)
  1435. xdg_toplevel_unset_fullscreen(window->wl.xdg.toplevel);
  1436. if (window->wl.current.toplevel_states & TOPLEVEL_STATE_MAXIMIZED)
  1437. xdg_toplevel_unset_maximized(window->wl.xdg.toplevel);
  1438. // There is no way to unset minimized, or even to know if we are
  1439. // minimized, so there is nothing to do in this case.
  1440. }
  1441. _glfwInputWindowMonitor(window, NULL);
  1442. }
  1443. void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
  1444. {
  1445. if (window->wl.xdg.toplevel) {
  1446. if (window->wl.wm_capabilities.maximize) xdg_toplevel_set_maximized(window->wl.xdg.toplevel);
  1447. else _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland compositor does not support maximizing windows");
  1448. }
  1449. }
  1450. void _glfwPlatformShowWindow(_GLFWwindow* window)
  1451. {
  1452. if (!window->wl.visible) {
  1453. create_window_desktop_surface(window);
  1454. window->wl.visible = true;
  1455. }
  1456. }
  1457. void _glfwPlatformHideWindow(_GLFWwindow* window)
  1458. {
  1459. if (window->wl.xdg.toplevel)
  1460. {
  1461. xdg_toplevel_destroy(window->wl.xdg.toplevel);
  1462. xdg_surface_destroy(window->wl.xdg.surface);
  1463. window->wl.xdg.toplevel = NULL;
  1464. window->wl.xdg.surface = NULL;
  1465. window->wl.once.surface_configured = false;
  1466. window->swaps_disallowed = true;
  1467. }
  1468. window->wl.visible = false;
  1469. }
  1470. static void
  1471. request_attention(GLFWwindow *window, const char *token, void *data UNUSED) {
  1472. if (window && token && token[0] && _glfw.wl.xdg_activation_v1) xdg_activation_v1_activate(_glfw.wl.xdg_activation_v1, token, ((_GLFWwindow*)window)->wl.surface);
  1473. }
  1474. static bool
  1475. has_activation_in_flight(_GLFWwindow* window, GLFWactivationcallback callback) {
  1476. for (size_t i = 0; i < _glfw.wl.activation_requests.sz; i++) {
  1477. glfw_wl_xdg_activation_request *r = _glfw.wl.activation_requests.array + i;
  1478. if (r->window_id == window->id && r->callback == callback) return true;
  1479. }
  1480. return false;
  1481. }
  1482. void _glfwPlatformRequestWindowAttention(_GLFWwindow* window) {
  1483. if (!has_activation_in_flight(window, request_attention)) get_activation_token(window, 0, request_attention, NULL);
  1484. }
  1485. int _glfwPlatformWindowBell(_GLFWwindow* window UNUSED)
  1486. {
  1487. // TODO: Use an actual Wayland API to implement this when one becomes available
  1488. return false;
  1489. }
  1490. static void
  1491. focus_window(GLFWwindow *window, const char *token, void *data UNUSED) {
  1492. if (!window) return;
  1493. if (token && token[0] && _glfw.wl.xdg_activation_v1) xdg_activation_v1_activate(_glfw.wl.xdg_activation_v1, token, ((_GLFWwindow*)window)->wl.surface);
  1494. else {
  1495. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: Window focus request via xdg-activation protocol was denied or is unsupported by the compositor. Use a better compositor.");
  1496. }
  1497. }
  1498. void _glfwPlatformFocusWindow(_GLFWwindow* window UNUSED)
  1499. {
  1500. // Attempt to focus the window by using the activation protocol, whether it works
  1501. // is entirely compositor dependent and as we all know Wayland and its ecosystem is
  1502. // the product of morons.
  1503. if (_glfw.wl.input_serial && !has_activation_in_flight(window, focus_window)) get_activation_token(window, _glfw.wl.input_serial, focus_window, NULL);
  1504. }
  1505. void _glfwPlatformSetWindowMonitor(_GLFWwindow* window,
  1506. _GLFWmonitor* monitor,
  1507. int xpos UNUSED, int ypos UNUSED,
  1508. int width UNUSED, int height UNUSED,
  1509. int refreshRate UNUSED)
  1510. {
  1511. setFullscreen(window, monitor, monitor != NULL);
  1512. _glfwInputWindowMonitor(window, monitor);
  1513. }
  1514. int _glfwPlatformWindowFocused(_GLFWwindow* window)
  1515. {
  1516. return _glfw.wl.keyboardFocusId == (window ? window->id : 0);
  1517. }
  1518. int _glfwPlatformWindowOccluded(_GLFWwindow* window UNUSED)
  1519. {
  1520. #ifdef XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION
  1521. return (window->wl.current.toplevel_states & TOPLEVEL_STATE_SUSPENDED) != 0;
  1522. #endif
  1523. return false;
  1524. }
  1525. int _glfwPlatformWindowIconified(_GLFWwindow* window UNUSED)
  1526. {
  1527. // xdg-shell doesn’t give any way to request whether a surface is
  1528. // iconified.
  1529. return false;
  1530. }
  1531. int _glfwPlatformWindowVisible(_GLFWwindow* window)
  1532. {
  1533. return window->wl.visible;
  1534. }
  1535. int _glfwPlatformWindowMaximized(_GLFWwindow* window)
  1536. {
  1537. return window->wl.current.toplevel_states & TOPLEVEL_STATE_MAXIMIZED;
  1538. }
  1539. int _glfwPlatformWindowHovered(_GLFWwindow* window)
  1540. {
  1541. return window->wl.hovered;
  1542. }
  1543. int _glfwPlatformFramebufferTransparent(_GLFWwindow* window)
  1544. {
  1545. return window->wl.transparent;
  1546. }
  1547. void _glfwPlatformSetWindowResizable(_GLFWwindow* window UNUSED, bool enabled UNUSED)
  1548. {
  1549. // TODO
  1550. _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
  1551. "Wayland: Window attribute setting not implemented yet");
  1552. }
  1553. void _glfwPlatformSetWindowFloating(_GLFWwindow* window UNUSED, bool enabled UNUSED)
  1554. {
  1555. // TODO
  1556. _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
  1557. "Wayland: Window attribute setting not implemented yet");
  1558. }
  1559. void _glfwPlatformSetWindowMousePassthrough(_GLFWwindow* window, bool enabled)
  1560. {
  1561. if (enabled)
  1562. {
  1563. struct wl_region* region = wl_compositor_create_region(_glfw.wl.compositor);
  1564. wl_surface_set_input_region(window->wl.surface, region);
  1565. wl_region_destroy(region);
  1566. }
  1567. else
  1568. wl_surface_set_input_region(window->wl.surface, 0);
  1569. commit_window_surface_if_safe(window);
  1570. }
  1571. float _glfwPlatformGetWindowOpacity(_GLFWwindow* window UNUSED)
  1572. {
  1573. return 1.f;
  1574. }
  1575. void _glfwPlatformSetWindowOpacity(_GLFWwindow* window UNUSED, float opacity UNUSED)
  1576. {
  1577. _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
  1578. "Wayland: The platform does not support setting the window opacity");
  1579. }
  1580. void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window UNUSED, bool enabled UNUSED)
  1581. {
  1582. // This is handled in relativePointerHandleRelativeMotion
  1583. }
  1584. bool _glfwPlatformRawMouseMotionSupported(void)
  1585. {
  1586. return true;
  1587. }
  1588. void _glfwPlatformPollEvents(void)
  1589. {
  1590. wl_display_dispatch_pending(_glfw.wl.display);
  1591. handleEvents(0);
  1592. }
  1593. void _glfwPlatformWaitEvents(void)
  1594. {
  1595. monotonic_t timeout = wl_display_dispatch_pending(_glfw.wl.display) > 0 ? 0 : -1;
  1596. handleEvents(timeout);
  1597. }
  1598. void _glfwPlatformWaitEventsTimeout(monotonic_t timeout)
  1599. {
  1600. if (wl_display_dispatch_pending(_glfw.wl.display) > 0) timeout = 0;
  1601. handleEvents(timeout);
  1602. }
  1603. void _glfwPlatformPostEmptyEvent(void)
  1604. {
  1605. wakeupEventLoop(&_glfw.wl.eventLoopData);
  1606. }
  1607. void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
  1608. {
  1609. if (xpos)
  1610. *xpos = window->wl.cursorPosX;
  1611. if (ypos)
  1612. *ypos = window->wl.cursorPosY;
  1613. }
  1614. static bool isPointerLocked(_GLFWwindow* window);
  1615. void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
  1616. {
  1617. if (isPointerLocked(window))
  1618. {
  1619. zwp_locked_pointer_v1_set_cursor_position_hint(
  1620. window->wl.pointerLock.lockedPointer,
  1621. wl_fixed_from_double(x), wl_fixed_from_double(y));
  1622. commit_window_surface_if_safe(window);
  1623. }
  1624. }
  1625. void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode UNUSED)
  1626. {
  1627. _glfwPlatformSetCursor(window, window->wl.currentCursor);
  1628. }
  1629. const char* _glfwPlatformGetNativeKeyName(int native_key)
  1630. {
  1631. return glfw_xkb_keysym_name(native_key);
  1632. }
  1633. int _glfwPlatformGetNativeKeyForKey(uint32_t key)
  1634. {
  1635. return glfw_xkb_sym_for_key(key);
  1636. }
  1637. int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
  1638. const GLFWimage* image,
  1639. int xhot, int yhot, int count UNUSED)
  1640. {
  1641. cursor->wl.buffer = createShmBuffer(image, false, true);
  1642. if (!cursor->wl.buffer)
  1643. return false;
  1644. cursor->wl.width = image->width;
  1645. cursor->wl.height = image->height;
  1646. cursor->wl.xhot = xhot;
  1647. cursor->wl.yhot = yhot;
  1648. cursor->wl.scale = -1;
  1649. cursor->wl.shape = GLFW_INVALID_CURSOR;
  1650. return true;
  1651. }
  1652. int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, GLFWCursorShape shape)
  1653. {
  1654. // Don't actually load the cursor at this point,
  1655. // because there's not enough info to be properly HiDPI aware.
  1656. cursor->wl.cursor = NULL;
  1657. cursor->wl.currentImage = 0;
  1658. cursor->wl.scale = 0;
  1659. cursor->wl.shape = shape;
  1660. return true;
  1661. }
  1662. void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
  1663. {
  1664. // If it's a standard cursor we don't need to do anything here
  1665. if (cursor->wl.cursor)
  1666. return;
  1667. if (cursor->wl.buffer)
  1668. wl_buffer_destroy(cursor->wl.buffer);
  1669. }
  1670. static void relativePointerHandleRelativeMotion(void* data,
  1671. struct zwp_relative_pointer_v1* pointer UNUSED,
  1672. uint32_t timeHi UNUSED,
  1673. uint32_t timeLo UNUSED,
  1674. wl_fixed_t dx,
  1675. wl_fixed_t dy,
  1676. wl_fixed_t dxUnaccel,
  1677. wl_fixed_t dyUnaccel)
  1678. {
  1679. _GLFWwindow* window = data;
  1680. double xpos = window->virtualCursorPosX;
  1681. double ypos = window->virtualCursorPosY;
  1682. if (window->cursorMode != GLFW_CURSOR_DISABLED)
  1683. return;
  1684. if (window->rawMouseMotion)
  1685. {
  1686. xpos += wl_fixed_to_double(dxUnaccel);
  1687. ypos += wl_fixed_to_double(dyUnaccel);
  1688. }
  1689. else
  1690. {
  1691. xpos += wl_fixed_to_double(dx);
  1692. ypos += wl_fixed_to_double(dy);
  1693. }
  1694. _glfwInputCursorPos(window, xpos, ypos);
  1695. }
  1696. static const struct zwp_relative_pointer_v1_listener relativePointerListener = {
  1697. relativePointerHandleRelativeMotion
  1698. };
  1699. static void lockedPointerHandleLocked(void* data UNUSED,
  1700. struct zwp_locked_pointer_v1* lockedPointer UNUSED)
  1701. {
  1702. }
  1703. static void unlockPointer(_GLFWwindow* window)
  1704. {
  1705. struct zwp_relative_pointer_v1* relativePointer =
  1706. window->wl.pointerLock.relativePointer;
  1707. struct zwp_locked_pointer_v1* lockedPointer =
  1708. window->wl.pointerLock.lockedPointer;
  1709. zwp_relative_pointer_v1_destroy(relativePointer);
  1710. zwp_locked_pointer_v1_destroy(lockedPointer);
  1711. window->wl.pointerLock.relativePointer = NULL;
  1712. window->wl.pointerLock.lockedPointer = NULL;
  1713. }
  1714. static void lockPointer(_GLFWwindow* window UNUSED);
  1715. static void lockedPointerHandleUnlocked(void* data UNUSED,
  1716. struct zwp_locked_pointer_v1* lockedPointer UNUSED)
  1717. {
  1718. }
  1719. static const struct zwp_locked_pointer_v1_listener lockedPointerListener = {
  1720. lockedPointerHandleLocked,
  1721. lockedPointerHandleUnlocked
  1722. };
  1723. static void lockPointer(_GLFWwindow* window)
  1724. {
  1725. struct zwp_relative_pointer_v1* relativePointer;
  1726. struct zwp_locked_pointer_v1* lockedPointer;
  1727. if (!_glfw.wl.relativePointerManager)
  1728. {
  1729. _glfwInputError(GLFW_PLATFORM_ERROR,
  1730. "Wayland: no relative pointer manager");
  1731. return;
  1732. }
  1733. relativePointer =
  1734. zwp_relative_pointer_manager_v1_get_relative_pointer(
  1735. _glfw.wl.relativePointerManager,
  1736. _glfw.wl.pointer);
  1737. zwp_relative_pointer_v1_add_listener(relativePointer,
  1738. &relativePointerListener,
  1739. window);
  1740. lockedPointer =
  1741. zwp_pointer_constraints_v1_lock_pointer(
  1742. _glfw.wl.pointerConstraints,
  1743. window->wl.surface,
  1744. _glfw.wl.pointer,
  1745. NULL,
  1746. ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
  1747. zwp_locked_pointer_v1_add_listener(lockedPointer,
  1748. &lockedPointerListener,
  1749. window);
  1750. window->wl.pointerLock.relativePointer = relativePointer;
  1751. window->wl.pointerLock.lockedPointer = lockedPointer;
  1752. set_cursor_surface(NULL, 0, 0, "lockPointer");
  1753. }
  1754. static bool isPointerLocked(_GLFWwindow* window)
  1755. {
  1756. return window->wl.pointerLock.lockedPointer != NULL;
  1757. }
  1758. void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
  1759. {
  1760. if (!_glfw.wl.pointer)
  1761. return;
  1762. window->wl.currentCursor = cursor;
  1763. // If we're not in the correct window just save the cursor
  1764. // the next time the pointer enters the window the cursor will change
  1765. if (window != _glfw.wl.pointerFocus || window->wl.decorations.focus != CENTRAL_WINDOW)
  1766. return;
  1767. // Unlock possible pointer lock if no longer disabled.
  1768. if (window->cursorMode != GLFW_CURSOR_DISABLED && isPointerLocked(window))
  1769. unlockPointer(window);
  1770. if (window->cursorMode == GLFW_CURSOR_NORMAL)
  1771. {
  1772. setCursorImage(window, false);
  1773. }
  1774. else if (window->cursorMode == GLFW_CURSOR_DISABLED)
  1775. {
  1776. if (!isPointerLocked(window))
  1777. lockPointer(window);
  1778. }
  1779. else if (window->cursorMode == GLFW_CURSOR_HIDDEN)
  1780. {
  1781. set_cursor_surface(NULL, 0, 0, "_glfwPlatformSetCursor");
  1782. }
  1783. }
  1784. static bool
  1785. write_all(int fd, const char *data, size_t sz) {
  1786. monotonic_t start = glfwGetTime();
  1787. size_t pos = 0;
  1788. while (pos < sz && glfwGetTime() - start < s_to_monotonic_t(2ll)) {
  1789. ssize_t ret = write(fd, data + pos, sz - pos);
  1790. if (ret < 0) {
  1791. if (errno == EAGAIN || errno == EINTR) continue;
  1792. _glfwInputError(GLFW_PLATFORM_ERROR,
  1793. "Wayland: Could not copy writing to destination fd failed with error: %s", strerror(errno));
  1794. return false;
  1795. }
  1796. if (ret > 0) {
  1797. start = glfwGetTime();
  1798. pos += ret;
  1799. }
  1800. }
  1801. return pos >= sz;
  1802. }
  1803. static void
  1804. send_clipboard_data(const _GLFWClipboardData *cd, const char *mime, int fd) {
  1805. 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";
  1806. GLFWDataChunk chunk = cd->get_data(mime, NULL, cd->ctype);
  1807. void *iter = chunk.iter;
  1808. if (!iter) return;
  1809. bool keep_going = true;
  1810. while (keep_going) {
  1811. chunk = cd->get_data(mime, iter, cd->ctype);
  1812. if (!chunk.sz) break;
  1813. if (!write_all(fd, chunk.data, chunk.sz)) keep_going = false;
  1814. if (chunk.free) chunk.free((void*)chunk.free_data);
  1815. }
  1816. cd->get_data(NULL, iter, cd->ctype);
  1817. }
  1818. static void _glfwSendClipboardText(void *data UNUSED, struct wl_data_source *data_source UNUSED, const char *mime_type, int fd) {
  1819. send_clipboard_data(&_glfw.clipboard, mime_type, fd);
  1820. close(fd);
  1821. }
  1822. static void _glfwSendPrimarySelectionText(void *data UNUSED, struct zwp_primary_selection_source_v1 *primary_selection_source UNUSED,
  1823. const char *mime_type, int fd) {
  1824. send_clipboard_data(&_glfw.primary, mime_type, fd);
  1825. close(fd);
  1826. }
  1827. static void
  1828. read_offer(int data_pipe, GLFWclipboardwritedatafun write_data, void *object) {
  1829. wl_display_flush(_glfw.wl.display);
  1830. struct pollfd fds;
  1831. fds.fd = data_pipe;
  1832. fds.events = POLLIN;
  1833. monotonic_t start = glfwGetTime();
  1834. #define bail(...) { \
  1835. _glfwInputError(GLFW_PLATFORM_ERROR, __VA_ARGS__); \
  1836. close(data_pipe); \
  1837. return; \
  1838. }
  1839. char buf[8192];
  1840. while (glfwGetTime() - start < s_to_monotonic_t(2ll)) {
  1841. int ret = poll(&fds, 1, 2000);
  1842. if (ret == -1) {
  1843. if (errno == EINTR) continue;
  1844. bail("Wayland: Failed to poll clipboard data from pipe with error: %s", strerror(errno));
  1845. }
  1846. if (!ret) {
  1847. bail("Wayland: Failed to read clipboard data from pipe (timed out)");
  1848. }
  1849. ret = read(data_pipe, buf, sizeof(buf));
  1850. if (ret == -1) {
  1851. if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) continue;
  1852. bail("Wayland: Failed to read clipboard data from pipe with error: %s", strerror(errno));
  1853. }
  1854. if (ret == 0) { close(data_pipe); return; }
  1855. if (!write_data(object, buf, ret)) bail("Wayland: call to write_data() failed with data from data offer");
  1856. start = glfwGetTime();
  1857. }
  1858. bail("Wayland: Failed to read clipboard data from pipe (timed out)");
  1859. #undef bail
  1860. }
  1861. typedef struct chunked_writer {
  1862. char *buf; size_t sz, cap;
  1863. } chunked_writer;
  1864. static bool
  1865. write_chunk(void *object, const char *data, size_t sz) {
  1866. chunked_writer *cw = object;
  1867. if (cw->cap < cw->sz + sz) {
  1868. cw->cap = MAX(cw->cap * 2, cw->sz + 8*sz);
  1869. cw->buf = realloc(cw->buf, cw->cap * sizeof(cw->buf[0]));
  1870. }
  1871. memcpy(cw->buf + cw->sz, data, sz);
  1872. cw->sz += sz;
  1873. return true;
  1874. }
  1875. static char*
  1876. read_offer_string(int data_pipe, size_t *sz) {
  1877. chunked_writer cw = {0};
  1878. read_offer(data_pipe, write_chunk, &cw);
  1879. if (cw.buf) {
  1880. *sz = cw.sz;
  1881. return cw.buf;
  1882. }
  1883. *sz = 0;
  1884. return NULL;
  1885. }
  1886. static void
  1887. read_clipboard_data_offer(struct wl_data_offer *data_offer, const char *mime, GLFWclipboardwritedatafun write_data, void *object) {
  1888. int pipefd[2];
  1889. if (pipe2(pipefd, O_CLOEXEC) != 0) return;
  1890. wl_data_offer_receive(data_offer, mime, pipefd[1]);
  1891. close(pipefd[1]);
  1892. read_offer(pipefd[0], write_data, object);
  1893. }
  1894. static void
  1895. read_primary_selection_offer(struct zwp_primary_selection_offer_v1 *primary_selection_offer, const char *mime, GLFWclipboardwritedatafun write_data, void *object) {
  1896. int pipefd[2];
  1897. if (pipe2(pipefd, O_CLOEXEC) != 0) return;
  1898. zwp_primary_selection_offer_v1_receive(primary_selection_offer, mime, pipefd[1]);
  1899. close(pipefd[1]);
  1900. read_offer(pipefd[0], write_data, object);
  1901. }
  1902. static char* read_data_offer(struct wl_data_offer *data_offer, const char *mime, size_t *sz) {
  1903. int pipefd[2];
  1904. if (pipe2(pipefd, O_CLOEXEC) != 0) return NULL;
  1905. wl_data_offer_receive(data_offer, mime, pipefd[1]);
  1906. close(pipefd[1]);
  1907. return read_offer_string(pipefd[0], sz);
  1908. }
  1909. static void data_source_canceled(void *data UNUSED, struct wl_data_source *wl_data_source) {
  1910. if (_glfw.wl.dataSourceForClipboard == wl_data_source) {
  1911. _glfw.wl.dataSourceForClipboard = NULL;
  1912. _glfw_free_clipboard_data(&_glfw.clipboard);
  1913. }
  1914. wl_data_source_destroy(wl_data_source);
  1915. }
  1916. static void primary_selection_source_canceled(void *data UNUSED, struct zwp_primary_selection_source_v1 *primary_selection_source) {
  1917. if (_glfw.wl.dataSourceForPrimarySelection == primary_selection_source) {
  1918. _glfw.wl.dataSourceForPrimarySelection = NULL;
  1919. _glfw_free_clipboard_data(&_glfw.primary);
  1920. }
  1921. zwp_primary_selection_source_v1_destroy(primary_selection_source);
  1922. }
  1923. // KWin aborts if we don't define these even though they are not used for copy/paste
  1924. static void dummy_data_source_target(void* data UNUSED, struct wl_data_source* wl_data_source UNUSED, const char* mime_type UNUSED) {
  1925. }
  1926. static void dummy_data_source_action(void* data UNUSED, struct wl_data_source* wl_data_source UNUSED, uint dnd_action UNUSED) {
  1927. }
  1928. static const struct wl_data_source_listener data_source_listener = {
  1929. .send = _glfwSendClipboardText,
  1930. .cancelled = data_source_canceled,
  1931. .target = dummy_data_source_target,
  1932. .action = dummy_data_source_action,
  1933. };
  1934. static const struct zwp_primary_selection_source_v1_listener primary_selection_source_listener = {
  1935. .send = _glfwSendPrimarySelectionText,
  1936. .cancelled = primary_selection_source_canceled,
  1937. };
  1938. void
  1939. destroy_data_offer(_GLFWWaylandDataOffer *offer) {
  1940. if (offer->id) {
  1941. if (offer->is_primary) zwp_primary_selection_offer_v1_destroy(offer->id);
  1942. else wl_data_offer_destroy(offer->id);
  1943. }
  1944. if (offer->mimes) {
  1945. for (size_t i = 0; i < offer->mimes_count; i++) free((char*)offer->mimes[i]);
  1946. free(offer->mimes);
  1947. }
  1948. memset(offer, 0, sizeof(_GLFWWaylandDataOffer));
  1949. }
  1950. static void prune_unclaimed_data_offers(void) {
  1951. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  1952. if (_glfw.wl.dataOffers[i].id && !_glfw.wl.dataOffers[i].offer_type) {
  1953. destroy_data_offer(&_glfw.wl.dataOffers[i]);
  1954. }
  1955. }
  1956. }
  1957. static void mark_selection_offer(void *data UNUSED, struct wl_data_device *data_device UNUSED, struct wl_data_offer *data_offer)
  1958. {
  1959. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  1960. if (_glfw.wl.dataOffers[i].id == data_offer) {
  1961. _glfw.wl.dataOffers[i].offer_type = CLIPBOARD;
  1962. } else if (_glfw.wl.dataOffers[i].offer_type == CLIPBOARD) {
  1963. _glfw.wl.dataOffers[i].offer_type = EXPIRED; // previous selection offer
  1964. }
  1965. }
  1966. prune_unclaimed_data_offers();
  1967. }
  1968. static void mark_primary_selection_offer(void *data UNUSED, struct zwp_primary_selection_device_v1* primary_selection_device UNUSED,
  1969. struct zwp_primary_selection_offer_v1 *primary_selection_offer) {
  1970. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  1971. if (_glfw.wl.dataOffers[i].id == primary_selection_offer) {
  1972. _glfw.wl.dataOffers[i].offer_type = PRIMARY_SELECTION;
  1973. } else if (_glfw.wl.dataOffers[i].offer_type == PRIMARY_SELECTION) {
  1974. _glfw.wl.dataOffers[i].offer_type = EXPIRED; // previous selection offer
  1975. }
  1976. }
  1977. prune_unclaimed_data_offers();
  1978. }
  1979. static void
  1980. set_offer_mimetype(_GLFWWaylandDataOffer* offer, const char* mime) {
  1981. if (strcmp(mime, clipboard_mime()) == 0) {
  1982. offer->is_self_offer = true;
  1983. }
  1984. if (!offer->mimes || offer->mimes_count >= offer->mimes_capacity - 1) {
  1985. offer->mimes = realloc(offer->mimes, sizeof(char*) * (offer->mimes_capacity + 64));
  1986. if (offer->mimes) offer->mimes_capacity += 64;
  1987. else return;
  1988. }
  1989. offer->mimes[offer->mimes_count++] = _glfw_strdup(mime);
  1990. }
  1991. static void handle_offer_mimetype(void *data UNUSED, struct wl_data_offer* id, const char *mime) {
  1992. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  1993. if (_glfw.wl.dataOffers[i].id == id) {
  1994. set_offer_mimetype(&_glfw.wl.dataOffers[i], mime);
  1995. break;
  1996. }
  1997. }
  1998. }
  1999. static void handle_primary_selection_offer_mimetype(void *data UNUSED, struct zwp_primary_selection_offer_v1* id, const char *mime) {
  2000. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2001. if (_glfw.wl.dataOffers[i].id == id) {
  2002. set_offer_mimetype((_GLFWWaylandDataOffer*)&_glfw.wl.dataOffers[i], mime);
  2003. break;
  2004. }
  2005. }
  2006. }
  2007. static void data_offer_source_actions(void *data UNUSED, struct wl_data_offer* id, uint32_t actions) {
  2008. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2009. if (_glfw.wl.dataOffers[i].id == id) {
  2010. _glfw.wl.dataOffers[i].source_actions = actions;
  2011. break;
  2012. }
  2013. }
  2014. }
  2015. static void data_offer_action(void *data UNUSED, struct wl_data_offer* id, uint32_t action) {
  2016. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2017. if (_glfw.wl.dataOffers[i].id == id) {
  2018. _glfw.wl.dataOffers[i].dnd_action = action;
  2019. break;
  2020. }
  2021. }
  2022. }
  2023. static const struct wl_data_offer_listener data_offer_listener = {
  2024. .offer = handle_offer_mimetype,
  2025. .source_actions = data_offer_source_actions,
  2026. .action = data_offer_action,
  2027. };
  2028. static const struct zwp_primary_selection_offer_v1_listener primary_selection_offer_listener = {
  2029. .offer = handle_primary_selection_offer_mimetype,
  2030. };
  2031. static size_t
  2032. handle_data_offer_generic(void *id, bool is_primary) {
  2033. size_t smallest_idx = SIZE_MAX, pos = 0;
  2034. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2035. if (_glfw.wl.dataOffers[i].idx && _glfw.wl.dataOffers[i].idx < smallest_idx) {
  2036. smallest_idx = _glfw.wl.dataOffers[i].idx;
  2037. pos = i;
  2038. }
  2039. if (_glfw.wl.dataOffers[i].id == NULL) {
  2040. pos = i;
  2041. goto end;
  2042. }
  2043. }
  2044. if (_glfw.wl.dataOffers[pos].id) destroy_data_offer(&_glfw.wl.dataOffers[pos]);
  2045. end:
  2046. _glfw.wl.dataOffers[pos].id = id;
  2047. _glfw.wl.dataOffers[pos].is_primary = is_primary;
  2048. _glfw.wl.dataOffers[pos].idx = ++_glfw.wl.dataOffersCounter;
  2049. return pos;
  2050. }
  2051. static void handle_data_offer(void *data UNUSED, struct wl_data_device *wl_data_device UNUSED, struct wl_data_offer *id) {
  2052. handle_data_offer_generic(id, false);
  2053. wl_data_offer_add_listener(id, &data_offer_listener, NULL);
  2054. }
  2055. 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) {
  2056. handle_data_offer_generic(id, true);
  2057. zwp_primary_selection_offer_v1_add_listener(id, &primary_selection_offer_listener, NULL);
  2058. }
  2059. 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) {
  2060. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2061. _GLFWWaylandDataOffer *d = _glfw.wl.dataOffers + i;
  2062. if (d->id == id) {
  2063. d->offer_type = DRAG_AND_DROP;
  2064. d->surface = surface;
  2065. _GLFWwindow* window = _glfw.windowListHead;
  2066. int format_priority = 0;
  2067. while (window)
  2068. {
  2069. if (window->wl.surface == surface) {
  2070. for (size_t j = 0; j < d->mimes_count; j++) {
  2071. int prio = _glfwInputDrop(window, d->mimes[j], NULL, 0);
  2072. if (prio > format_priority) d->mime_for_drop = d->mimes[j];
  2073. }
  2074. break;
  2075. }
  2076. window = window->next;
  2077. }
  2078. wl_data_offer_accept(id, serial, d->mime_for_drop);
  2079. } else if (_glfw.wl.dataOffers[i].offer_type == DRAG_AND_DROP) {
  2080. _glfw.wl.dataOffers[i].offer_type = EXPIRED; // previous drag offer
  2081. }
  2082. }
  2083. prune_unclaimed_data_offers();
  2084. }
  2085. static void drag_leave(void *data UNUSED, struct wl_data_device *wl_data_device UNUSED) {
  2086. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2087. if (_glfw.wl.dataOffers[i].offer_type == DRAG_AND_DROP) {
  2088. destroy_data_offer(&_glfw.wl.dataOffers[i]);
  2089. }
  2090. }
  2091. }
  2092. static void drop(void *data UNUSED, struct wl_data_device *wl_data_device UNUSED) {
  2093. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2094. if (_glfw.wl.dataOffers[i].offer_type == DRAG_AND_DROP && _glfw.wl.dataOffers[i].mime_for_drop) {
  2095. size_t sz = 0;
  2096. char *d = read_data_offer(_glfw.wl.dataOffers[i].id, _glfw.wl.dataOffers[i].mime_for_drop, &sz);
  2097. if (d) {
  2098. // We dont do finish as this requires version 3 for wl_data_device_manager
  2099. // which then requires more work with calling set_actions for drag and drop to function
  2100. // wl_data_offer_finish(_glfw.wl.dataOffers[i].id);
  2101. _GLFWwindow* window = _glfw.windowListHead;
  2102. while (window)
  2103. {
  2104. if (window->wl.surface == _glfw.wl.dataOffers[i].surface) {
  2105. _glfwInputDrop(window, _glfw.wl.dataOffers[i].mime_for_drop, d, sz);
  2106. break;
  2107. }
  2108. window = window->next;
  2109. }
  2110. free(d);
  2111. }
  2112. destroy_data_offer(&_glfw.wl.dataOffers[i]);
  2113. break;
  2114. }
  2115. }
  2116. }
  2117. 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) {
  2118. }
  2119. static const struct wl_data_device_listener data_device_listener = {
  2120. .data_offer = handle_data_offer,
  2121. .selection = mark_selection_offer,
  2122. .enter = drag_enter,
  2123. .motion = motion,
  2124. .drop = drop,
  2125. .leave = drag_leave,
  2126. };
  2127. static const struct zwp_primary_selection_device_v1_listener primary_selection_device_listener = {
  2128. .data_offer = handle_primary_selection_offer,
  2129. .selection = mark_primary_selection_offer,
  2130. };
  2131. void _glfwSetupWaylandDataDevice(void) {
  2132. _glfw.wl.dataDevice = wl_data_device_manager_get_data_device(_glfw.wl.dataDeviceManager, _glfw.wl.seat);
  2133. if (_glfw.wl.dataDevice) wl_data_device_add_listener(_glfw.wl.dataDevice, &data_device_listener, NULL);
  2134. }
  2135. void _glfwSetupWaylandPrimarySelectionDevice(void) {
  2136. _glfw.wl.primarySelectionDevice = zwp_primary_selection_device_manager_v1_get_device(_glfw.wl.primarySelectionDeviceManager, _glfw.wl.seat);
  2137. if (_glfw.wl.primarySelectionDevice) zwp_primary_selection_device_v1_add_listener(_glfw.wl.primarySelectionDevice, &primary_selection_device_listener, NULL);
  2138. }
  2139. static bool _glfwEnsureDataDevice(void) {
  2140. if (!_glfw.wl.dataDeviceManager)
  2141. {
  2142. _glfwInputError(GLFW_PLATFORM_ERROR,
  2143. "Wayland: Cannot use clipboard, data device manager is not ready");
  2144. return false;
  2145. }
  2146. if (!_glfw.wl.dataDevice)
  2147. {
  2148. if (!_glfw.wl.seat)
  2149. {
  2150. _glfwInputError(GLFW_PLATFORM_ERROR,
  2151. "Wayland: Cannot use clipboard, seat is not ready");
  2152. return false;
  2153. }
  2154. if (!_glfw.wl.dataDevice)
  2155. {
  2156. _glfwInputError(GLFW_PLATFORM_ERROR,
  2157. "Wayland: Cannot use clipboard, failed to create data device");
  2158. return false;
  2159. }
  2160. }
  2161. return true;
  2162. }
  2163. typedef void(*add_offer_func)(void*, const char *mime);
  2164. void
  2165. _glfwPlatformSetClipboard(GLFWClipboardType t) {
  2166. _GLFWClipboardData *cd = NULL;
  2167. void *data_source;
  2168. add_offer_func f;
  2169. if (t == GLFW_CLIPBOARD) {
  2170. if (!_glfwEnsureDataDevice()) return;
  2171. cd = &_glfw.clipboard;
  2172. f = (add_offer_func)wl_data_source_offer;
  2173. if (_glfw.wl.dataSourceForClipboard) wl_data_source_destroy(_glfw.wl.dataSourceForClipboard);
  2174. _glfw.wl.dataSourceForClipboard = wl_data_device_manager_create_data_source(_glfw.wl.dataDeviceManager);
  2175. if (!_glfw.wl.dataSourceForClipboard)
  2176. {
  2177. _glfwInputError(GLFW_PLATFORM_ERROR,
  2178. "Wayland: Cannot copy failed to create data source");
  2179. return;
  2180. }
  2181. wl_data_source_add_listener(_glfw.wl.dataSourceForClipboard, &data_source_listener, NULL);
  2182. data_source = _glfw.wl.dataSourceForClipboard;
  2183. } else {
  2184. if (!_glfw.wl.primarySelectionDevice) {
  2185. static bool warned_about_primary_selection_device = false;
  2186. if (!warned_about_primary_selection_device) {
  2187. _glfwInputError(GLFW_PLATFORM_ERROR,
  2188. "Wayland: Cannot copy no primary selection device available");
  2189. warned_about_primary_selection_device = true;
  2190. }
  2191. return;
  2192. }
  2193. cd = &_glfw.primary;
  2194. f = (add_offer_func)zwp_primary_selection_source_v1_offer;
  2195. if (_glfw.wl.dataSourceForPrimarySelection) zwp_primary_selection_source_v1_destroy(_glfw.wl.dataSourceForPrimarySelection);
  2196. _glfw.wl.dataSourceForPrimarySelection = zwp_primary_selection_device_manager_v1_create_source(_glfw.wl.primarySelectionDeviceManager);
  2197. if (!_glfw.wl.dataSourceForPrimarySelection)
  2198. {
  2199. _glfwInputError(GLFW_PLATFORM_ERROR,
  2200. "Wayland: Cannot copy failed to create primary selection source");
  2201. return;
  2202. }
  2203. zwp_primary_selection_source_v1_add_listener(_glfw.wl.dataSourceForPrimarySelection, &primary_selection_source_listener, NULL);
  2204. data_source = _glfw.wl.dataSourceForPrimarySelection;
  2205. }
  2206. f(data_source, clipboard_mime());
  2207. for (size_t i = 0; i < cd->num_mime_types; i++) {
  2208. if (strcmp(cd->mime_types[i], "text/plain") == 0) {
  2209. f(data_source, "TEXT");
  2210. f(data_source, "STRING");
  2211. f(data_source, "UTF8_STRING");
  2212. f(data_source, "text/plain;charset=utf-8");
  2213. }
  2214. f(data_source, cd->mime_types[i]);
  2215. }
  2216. if (t == GLFW_CLIPBOARD) {
  2217. // According to some interpretations of the Wayland spec only the application that has keyboard focus can set the clipboard.
  2218. // Hurray for the Wayland nanny state!
  2219. //
  2220. // However in wl-roots based compositors, using the serial from the keyboard enter event doesn't work. No clue what
  2221. // the correct serial to use here is. Given this Wayland there probably isn't one. What a joke.
  2222. // Bug report: https://github.com/kovidgoyal/kitty/issues/6890
  2223. // Ironically one of the contributors to wl_roots claims the keyboard enter serial is the correct one to use:
  2224. // https://emersion.fr/blog/2020/wayland-clipboard-drag-and-drop/
  2225. // The Wayland spec itself says "serial number of the event that triggered this request"
  2226. // https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_data_device
  2227. // So who the fuck knows. Just use the latest received serial and ask anybody that uses Wayland
  2228. // to get their head examined.
  2229. wl_data_device_set_selection(_glfw.wl.dataDevice, _glfw.wl.dataSourceForClipboard, _glfw.wl.serial);
  2230. } else {
  2231. // According to the Wayland spec we can only set the primary selection in response to a pointer button event
  2232. // Hurray for the Wayland nanny state!
  2233. zwp_primary_selection_device_v1_set_selection(
  2234. _glfw.wl.primarySelectionDevice, _glfw.wl.dataSourceForPrimarySelection, _glfw.wl.pointer_serial);
  2235. }
  2236. }
  2237. static bool
  2238. offer_has_mime(const _GLFWWaylandDataOffer *d, const char *mime) {
  2239. for (unsigned i = 0; i < d->mimes_count; i++) {
  2240. if (strcmp(d->mimes[i], mime) == 0) return true;
  2241. }
  2242. return false;
  2243. }
  2244. static const char*
  2245. plain_text_mime_for_offer(const _GLFWWaylandDataOffer *d) {
  2246. #define A(x) if (offer_has_mime(d, x)) return x;
  2247. A("text/plain;charset=utf-8");
  2248. A("text/plain");
  2249. A("UTF8_STRING");
  2250. A("STRING");
  2251. A("TEXT");
  2252. #undef A
  2253. return NULL;
  2254. }
  2255. void
  2256. _glfwPlatformGetClipboard(GLFWClipboardType clipboard_type, const char* mime_type, GLFWclipboardwritedatafun write_data, void *object) {
  2257. _GLFWWaylandOfferType offer_type = clipboard_type == GLFW_PRIMARY_SELECTION ? PRIMARY_SELECTION : CLIPBOARD;
  2258. for (size_t i = 0; i < arraysz(_glfw.wl.dataOffers); i++) {
  2259. _GLFWWaylandDataOffer *d = _glfw.wl.dataOffers + i;
  2260. if (d->id && d->offer_type == offer_type) {
  2261. if (d->is_self_offer) {
  2262. write_data(object, NULL, 1);
  2263. return;
  2264. }
  2265. if (mime_type == NULL) {
  2266. bool ok = true;
  2267. for (size_t o = 0; o < d->mimes_count; o++) {
  2268. const char *q = d->mimes[o];
  2269. if (strchr(d->mimes[0], '/')) {
  2270. if (strcmp(q, clipboard_mime()) == 0) continue;
  2271. if (strcmp(q, "text/plain;charset=utf-8") == 0) q = "text/plain";
  2272. } else {
  2273. if (strcmp(q, "UTF8_STRING") == 0 || strcmp(q, "STRING") == 0 || strcmp(q, "TEXT") == 0) q = "text/plain";
  2274. }
  2275. if (ok) ok = write_data(object, q, strlen(q));
  2276. }
  2277. return;
  2278. }
  2279. if (strcmp(mime_type, "text/plain") == 0) {
  2280. mime_type = plain_text_mime_for_offer(d);
  2281. if (!mime_type) return;
  2282. }
  2283. if (d->is_primary) {
  2284. read_primary_selection_offer(d->id, mime_type, write_data, object);
  2285. } else {
  2286. read_clipboard_data_offer(d->id, mime_type, write_data, object);
  2287. }
  2288. break;
  2289. }
  2290. }
  2291. }
  2292. EGLenum _glfwPlatformGetEGLPlatform(EGLint** attribs UNUSED)
  2293. {
  2294. if (_glfw.egl.EXT_platform_base && _glfw.egl.EXT_platform_wayland)
  2295. return EGL_PLATFORM_WAYLAND_EXT;
  2296. else
  2297. return 0;
  2298. }
  2299. EGLNativeDisplayType _glfwPlatformGetEGLNativeDisplay(void)
  2300. {
  2301. return _glfw.wl.display;
  2302. }
  2303. EGLNativeWindowType _glfwPlatformGetEGLNativeWindow(_GLFWwindow* window)
  2304. {
  2305. return window->wl.native;
  2306. }
  2307. void _glfwPlatformGetRequiredInstanceExtensions(char** extensions)
  2308. {
  2309. if (!_glfw.vk.KHR_surface || !_glfw.vk.KHR_wayland_surface)
  2310. return;
  2311. extensions[0] = "VK_KHR_surface";
  2312. extensions[1] = "VK_KHR_wayland_surface";
  2313. }
  2314. int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance,
  2315. VkPhysicalDevice device,
  2316. uint32_t queuefamily)
  2317. {
  2318. PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR
  2319. vkGetPhysicalDeviceWaylandPresentationSupportKHR =
  2320. (PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)
  2321. vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceWaylandPresentationSupportKHR");
  2322. if (!vkGetPhysicalDeviceWaylandPresentationSupportKHR)
  2323. {
  2324. _glfwInputError(GLFW_API_UNAVAILABLE,
  2325. "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension");
  2326. return VK_NULL_HANDLE;
  2327. }
  2328. return vkGetPhysicalDeviceWaylandPresentationSupportKHR(device,
  2329. queuefamily,
  2330. _glfw.wl.display);
  2331. }
  2332. VkResult _glfwPlatformCreateWindowSurface(VkInstance instance,
  2333. _GLFWwindow* window,
  2334. const VkAllocationCallbacks* allocator,
  2335. VkSurfaceKHR* surface)
  2336. {
  2337. VkResult err;
  2338. VkWaylandSurfaceCreateInfoKHR sci;
  2339. PFN_vkCreateWaylandSurfaceKHR vkCreateWaylandSurfaceKHR;
  2340. vkCreateWaylandSurfaceKHR = (PFN_vkCreateWaylandSurfaceKHR)
  2341. vkGetInstanceProcAddr(instance, "vkCreateWaylandSurfaceKHR");
  2342. if (!vkCreateWaylandSurfaceKHR)
  2343. {
  2344. _glfwInputError(GLFW_API_UNAVAILABLE,
  2345. "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension");
  2346. return VK_ERROR_EXTENSION_NOT_PRESENT;
  2347. }
  2348. memset(&sci, 0, sizeof(sci));
  2349. sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
  2350. sci.display = _glfw.wl.display;
  2351. sci.surface = window->wl.surface;
  2352. err = vkCreateWaylandSurfaceKHR(instance, &sci, allocator, surface);
  2353. if (err)
  2354. {
  2355. _glfwInputError(GLFW_PLATFORM_ERROR,
  2356. "Wayland: Failed to create Vulkan surface: %s",
  2357. _glfwGetVulkanResultString(err));
  2358. }
  2359. return err;
  2360. }
  2361. static void
  2362. frame_handle_redraw(void *data, struct wl_callback *callback, uint32_t time UNUSED) {
  2363. _GLFWwindow* window = (_GLFWwindow*) data;
  2364. if (callback == window->wl.frameCallbackData.current_wl_callback) {
  2365. window->wl.frameCallbackData.callback(window->wl.frameCallbackData.id);
  2366. window->wl.frameCallbackData.current_wl_callback = NULL;
  2367. }
  2368. wl_callback_destroy(callback);
  2369. }
  2370. void
  2371. _glfwPlatformChangeCursorTheme(void) {
  2372. glfw_wlc_destroy();
  2373. _GLFWwindow *w = _glfw.windowListHead;
  2374. while (w) {
  2375. setCursorImage(w, true);
  2376. w = w->next;
  2377. }
  2378. }
  2379. int
  2380. _glfwPlatformSetWindowBlur(_GLFWwindow *window, int blur_radius) {
  2381. if (!window->wl.transparent) return 0;
  2382. bool has_blur = window->wl.has_blur;
  2383. bool new_has_blur = blur_radius > 0;
  2384. if (new_has_blur != has_blur) {
  2385. window->wl.has_blur = new_has_blur;
  2386. update_regions(window);
  2387. }
  2388. return has_blur ? 1 : 0;
  2389. }
  2390. //////////////////////////////////////////////////////////////////////////
  2391. ////// GLFW native API //////
  2392. //////////////////////////////////////////////////////////////////////////
  2393. GLFWAPI struct wl_display* glfwGetWaylandDisplay(void)
  2394. {
  2395. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  2396. return _glfw.wl.display;
  2397. }
  2398. GLFWAPI struct wl_surface* glfwGetWaylandWindow(GLFWwindow* handle)
  2399. {
  2400. _GLFWwindow* window = (_GLFWwindow*) handle;
  2401. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  2402. return window->wl.surface;
  2403. }
  2404. GLFWAPI void glfwWaylandActivateWindow(GLFWwindow* handle, const char *activation_token) {
  2405. _GLFWwindow* window = (_GLFWwindow*) handle;
  2406. _GLFW_REQUIRE_INIT();
  2407. 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);
  2408. }
  2409. GLFWAPI void glfwWaylandRunWithActivationToken(GLFWwindow *handle, GLFWactivationcallback cb, void *cb_data) {
  2410. _GLFWwindow* window = (_GLFWwindow*) handle;
  2411. _GLFW_REQUIRE_INIT();
  2412. get_activation_token(window, _glfw.wl.input_serial, cb, cb_data);
  2413. }
  2414. GLFWAPI int glfwGetNativeKeyForName(const char* keyName, bool caseSensitive) {
  2415. return glfw_xkb_keysym_from_name(keyName, caseSensitive);
  2416. }
  2417. GLFWAPI void glfwRequestWaylandFrameEvent(GLFWwindow *handle, unsigned long long id, void(*callback)(unsigned long long id)) {
  2418. _GLFWwindow* window = (_GLFWwindow*) handle;
  2419. static const struct wl_callback_listener frame_listener = { .done = frame_handle_redraw };
  2420. if (window->wl.frameCallbackData.current_wl_callback) wl_callback_destroy(window->wl.frameCallbackData.current_wl_callback);
  2421. if (window->wl.waiting_for_swap_to_commit) {
  2422. callback(id);
  2423. window->wl.frameCallbackData.id = 0;
  2424. window->wl.frameCallbackData.callback = NULL;
  2425. window->wl.frameCallbackData.current_wl_callback = NULL;
  2426. } else {
  2427. window->wl.frameCallbackData.id = id;
  2428. window->wl.frameCallbackData.callback = callback;
  2429. window->wl.frameCallbackData.current_wl_callback = wl_surface_frame(window->wl.surface);
  2430. if (window->wl.frameCallbackData.current_wl_callback) {
  2431. wl_callback_add_listener(window->wl.frameCallbackData.current_wl_callback, &frame_listener, window);
  2432. commit_window_surface_if_safe(window);
  2433. }
  2434. }
  2435. }
  2436. GLFWAPI unsigned long long glfwDBusUserNotify(const GLFWDBUSNotificationData *n, GLFWDBusnotificationcreatedfun callback, void *data) {
  2437. return glfw_dbus_send_user_notification(n, callback, data);
  2438. }
  2439. GLFWAPI void glfwDBusSetUserNotificationHandler(GLFWDBusnotificationactivatedfun handler) {
  2440. glfw_dbus_set_user_notification_activated_handler(handler);
  2441. }
  2442. GLFWAPI bool glfwWaylandSetTitlebarColor(GLFWwindow *handle, uint32_t color, bool use_system_color) {
  2443. _GLFWwindow* window = (_GLFWwindow*) handle;
  2444. if (!window->wl.decorations.serverSide) {
  2445. csd_set_titlebar_color(window, color, use_system_color);
  2446. return true;
  2447. }
  2448. return false;
  2449. }
  2450. GLFWAPI void glfwWaylandRedrawCSDWindowTitle(GLFWwindow *handle) {
  2451. _GLFWwindow* window = (_GLFWwindow*) handle;
  2452. if (csd_change_title(window)) commit_window_surface_if_safe(window);
  2453. }
  2454. GLFWAPI void glfwWaylandSetupLayerShellForNextWindow(const GLFWLayerShellConfig *c) {
  2455. layer_shell_config_for_next_window = *c;
  2456. }
  2457. void
  2458. _glfwPlatformInputColorScheme(GLFWColorScheme appearance UNUSED) {
  2459. _GLFWwindow* window = _glfw.windowListHead;
  2460. while (window) {
  2461. glfwWaylandRedrawCSDWindowTitle((GLFWwindow*)window);
  2462. window = window->next;
  2463. }
  2464. }