wl_window.c 108 KB

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