wl_window.c 107 KB

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