wl_window.c 108 KB

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