wl_window.c 107 KB

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