SDL_waylandevents.c 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_VIDEO_DRIVER_WAYLAND
  20. #include "../../core/unix/SDL_poll.h"
  21. #include "../../events/SDL_events_c.h"
  22. #include "../../events/SDL_scancode_tables_c.h"
  23. #include "../../events/SDL_keysym_to_keycode_c.h"
  24. #include "../../core/linux/SDL_system_theme.h"
  25. #include "../SDL_sysvideo.h"
  26. #include "SDL_waylandvideo.h"
  27. #include "SDL_waylandevents_c.h"
  28. #include "SDL_waylandwindow.h"
  29. #include "SDL_waylandmouse.h"
  30. #include "pointer-constraints-unstable-v1-client-protocol.h"
  31. #include "relative-pointer-unstable-v1-client-protocol.h"
  32. #include "xdg-shell-client-protocol.h"
  33. #include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h"
  34. #include "text-input-unstable-v3-client-protocol.h"
  35. #include "tablet-v2-client-protocol.h"
  36. #include "primary-selection-unstable-v1-client-protocol.h"
  37. #include "input-timestamps-unstable-v1-client-protocol.h"
  38. #ifdef HAVE_LIBDECOR_H
  39. #include <libdecor.h>
  40. #endif
  41. #ifdef SDL_INPUT_LINUXEV
  42. #include <linux/input.h>
  43. #else
  44. #define BTN_LEFT (0x110)
  45. #define BTN_RIGHT (0x111)
  46. #define BTN_MIDDLE (0x112)
  47. #define BTN_SIDE (0x113)
  48. #define BTN_EXTRA (0x114)
  49. #endif
  50. #include "../../events/SDL_keysym_to_scancode_c.h"
  51. #include "../../events/imKStoUCS.h"
  52. #include <errno.h>
  53. #include <sys/mman.h>
  54. #include <unistd.h>
  55. #include <xkbcommon/xkbcommon-compose.h>
  56. #include <xkbcommon/xkbcommon.h>
  57. #include "cursor-shape-v1-client-protocol.h"
  58. // Weston uses a ratio of 10 units per scroll tick
  59. #define WAYLAND_WHEEL_AXIS_UNIT 10
  60. #ifndef XKB_MOD_NAME_MOD3
  61. #define XKB_MOD_NAME_MOD3 "Mod3"
  62. #endif
  63. #ifndef XKB_MOD_NAME_MOD5
  64. #define XKB_MOD_NAME_MOD5 "Mod5"
  65. #endif
  66. // Keyboard and mouse names to match XWayland
  67. #define WAYLAND_DEFAULT_KEYBOARD_NAME "Virtual core keyboard"
  68. #define WAYLAND_DEFAULT_POINTER_NAME "Virtual core pointer"
  69. // Focus clickthrough timeout
  70. #define WAYLAND_FOCUS_CLICK_TIMEOUT_NS SDL_MS_TO_NS(10)
  71. struct SDL_WaylandTouchPoint
  72. {
  73. SDL_TouchID id;
  74. wl_fixed_t fx;
  75. wl_fixed_t fy;
  76. struct wl_surface *surface;
  77. struct wl_list link;
  78. };
  79. static struct wl_list touch_points;
  80. static void touch_add(SDL_TouchID id, wl_fixed_t fx, wl_fixed_t fy, struct wl_surface *surface)
  81. {
  82. struct SDL_WaylandTouchPoint *tp = SDL_malloc(sizeof(struct SDL_WaylandTouchPoint));
  83. SDL_zerop(tp);
  84. tp->id = id;
  85. tp->fx = fx;
  86. tp->fy = fy;
  87. tp->surface = surface;
  88. WAYLAND_wl_list_insert(&touch_points, &tp->link);
  89. }
  90. static void touch_update(SDL_TouchID id, wl_fixed_t fx, wl_fixed_t fy, struct wl_surface **surface)
  91. {
  92. struct SDL_WaylandTouchPoint *tp;
  93. wl_list_for_each (tp, &touch_points, link) {
  94. if (tp->id == id) {
  95. tp->fx = fx;
  96. tp->fy = fy;
  97. if (surface) {
  98. *surface = tp->surface;
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. static void touch_del(SDL_TouchID id, wl_fixed_t *fx, wl_fixed_t *fy, struct wl_surface **surface)
  105. {
  106. struct SDL_WaylandTouchPoint *tp;
  107. wl_list_for_each (tp, &touch_points, link) {
  108. if (tp->id == id) {
  109. if (fx) {
  110. *fx = tp->fx;
  111. }
  112. if (fy) {
  113. *fy = tp->fy;
  114. }
  115. if (surface) {
  116. *surface = tp->surface;
  117. }
  118. WAYLAND_wl_list_remove(&tp->link);
  119. SDL_free(tp);
  120. break;
  121. }
  122. }
  123. }
  124. static bool Wayland_SurfaceHasActiveTouches(struct wl_surface *surface)
  125. {
  126. struct SDL_WaylandTouchPoint *tp;
  127. wl_list_for_each (tp, &touch_points, link) {
  128. if (tp->surface == surface) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. static Uint64 Wayland_GetEventTimestamp(Uint64 nsTimestamp)
  135. {
  136. static Uint64 timestamp_offset = 0;
  137. const Uint64 now = SDL_GetTicksNS();
  138. if (!timestamp_offset) {
  139. timestamp_offset = (now - nsTimestamp);
  140. }
  141. nsTimestamp += timestamp_offset;
  142. if (nsTimestamp > now) {
  143. timestamp_offset -= (nsTimestamp - now);
  144. nsTimestamp = now;
  145. }
  146. return nsTimestamp;
  147. }
  148. static void Wayland_input_timestamp_listener(void *data, struct zwp_input_timestamps_v1 *zwp_input_timestamps_v1,
  149. uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec)
  150. {
  151. *((Uint64 *)data) = ((((Uint64)tv_sec_hi << 32) | (Uint64)tv_sec_lo) * SDL_NS_PER_SECOND) + tv_nsec;
  152. }
  153. static const struct zwp_input_timestamps_v1_listener timestamp_listener = {
  154. Wayland_input_timestamp_listener
  155. };
  156. static Uint64 Wayland_EventTimestampMSToNS(Uint32 wl_timestamp_ms)
  157. {
  158. static Uint64 timestamp_offset = 0;
  159. static Uint32 last = 0;
  160. // Handle 32-bit timer rollover.
  161. if (wl_timestamp_ms < last) {
  162. timestamp_offset += SDL_MS_TO_NS(SDL_UINT64_C(0x100000000));
  163. }
  164. last = wl_timestamp_ms;
  165. return SDL_MS_TO_NS(wl_timestamp_ms) + timestamp_offset;
  166. }
  167. static Uint64 Wayland_GetKeyboardTimestamp(struct SDL_WaylandInput *input, Uint32 wl_timestamp_ms)
  168. {
  169. if (wl_timestamp_ms) {
  170. return Wayland_GetEventTimestamp(input->keyboard_timestamp_ns ? input->keyboard_timestamp_ns : Wayland_EventTimestampMSToNS(wl_timestamp_ms));
  171. }
  172. return 0;
  173. }
  174. static Uint64 Wayland_GetPointerTimestamp(struct SDL_WaylandInput *input, Uint32 wl_timestamp_ms)
  175. {
  176. if (wl_timestamp_ms) {
  177. return Wayland_GetEventTimestamp(input->pointer_timestamp_ns ? input->pointer_timestamp_ns : Wayland_EventTimestampMSToNS(wl_timestamp_ms));
  178. }
  179. return 0;
  180. }
  181. Uint64 Wayland_GetTouchTimestamp(struct SDL_WaylandInput *input, Uint32 wl_timestamp_ms)
  182. {
  183. if (wl_timestamp_ms) {
  184. return Wayland_GetEventTimestamp(input->touch_timestamp_ns ? input->touch_timestamp_ns : Wayland_EventTimestampMSToNS(wl_timestamp_ms));
  185. }
  186. return 0;
  187. }
  188. void Wayland_RegisterTimestampListeners(struct SDL_WaylandInput *input)
  189. {
  190. SDL_VideoData *viddata = input->display;
  191. if (viddata->input_timestamps_manager) {
  192. if (input->keyboard && !input->keyboard_timestamps) {
  193. input->keyboard_timestamps = zwp_input_timestamps_manager_v1_get_keyboard_timestamps(viddata->input_timestamps_manager, input->keyboard);
  194. zwp_input_timestamps_v1_add_listener(input->keyboard_timestamps, &timestamp_listener, &input->keyboard_timestamp_ns);
  195. }
  196. if (input->pointer && !input->pointer_timestamps) {
  197. input->pointer_timestamps = zwp_input_timestamps_manager_v1_get_pointer_timestamps(viddata->input_timestamps_manager, input->pointer);
  198. zwp_input_timestamps_v1_add_listener(input->pointer_timestamps, &timestamp_listener, &input->pointer_timestamp_ns);
  199. }
  200. if (input->touch && !input->touch_timestamps) {
  201. input->touch_timestamps = zwp_input_timestamps_manager_v1_get_touch_timestamps(viddata->input_timestamps_manager, input->touch);
  202. zwp_input_timestamps_v1_add_listener(input->touch_timestamps, &timestamp_listener, &input->touch_timestamp_ns);
  203. }
  204. }
  205. }
  206. void Wayland_CreateCursorShapeDevice(struct SDL_WaylandInput *input)
  207. {
  208. SDL_VideoData *viddata = input->display;
  209. if (viddata->cursor_shape_manager) {
  210. if (input->pointer && !input->cursor_shape) {
  211. input->cursor_shape = wp_cursor_shape_manager_v1_get_pointer(viddata->cursor_shape_manager, input->pointer);
  212. }
  213. }
  214. }
  215. // Returns true if a key repeat event was due
  216. static bool keyboard_repeat_handle(SDL_WaylandKeyboardRepeat *repeat_info, Uint64 elapsed)
  217. {
  218. bool ret = false;
  219. while (elapsed >= repeat_info->next_repeat_ns) {
  220. if (repeat_info->scancode != SDL_SCANCODE_UNKNOWN) {
  221. const Uint64 timestamp = repeat_info->base_time_ns + repeat_info->next_repeat_ns;
  222. SDL_SendKeyboardKeyIgnoreModifiers(timestamp, repeat_info->keyboard_id, repeat_info->key, repeat_info->scancode, true);
  223. }
  224. if (repeat_info->text[0]) {
  225. SDL_SendKeyboardText(repeat_info->text);
  226. }
  227. repeat_info->next_repeat_ns += SDL_NS_PER_SECOND / (Uint64)repeat_info->repeat_rate;
  228. ret = true;
  229. }
  230. return ret;
  231. }
  232. static void keyboard_repeat_clear(SDL_WaylandKeyboardRepeat *repeat_info)
  233. {
  234. if (!repeat_info->is_initialized) {
  235. return;
  236. }
  237. repeat_info->is_key_down = false;
  238. }
  239. static void keyboard_repeat_set(SDL_WaylandKeyboardRepeat *repeat_info, Uint32 keyboard_id, uint32_t key, Uint32 wl_press_time_ms,
  240. Uint64 base_time_ns, uint32_t scancode, bool has_text, char text[8])
  241. {
  242. if (!repeat_info->is_initialized || !repeat_info->repeat_rate) {
  243. return;
  244. }
  245. repeat_info->is_key_down = true;
  246. repeat_info->keyboard_id = keyboard_id;
  247. repeat_info->key = key;
  248. repeat_info->wl_press_time_ms = wl_press_time_ms;
  249. repeat_info->base_time_ns = base_time_ns;
  250. repeat_info->sdl_press_time_ns = SDL_GetTicksNS();
  251. repeat_info->next_repeat_ns = SDL_MS_TO_NS(repeat_info->repeat_delay_ms);
  252. repeat_info->scancode = scancode;
  253. if (has_text) {
  254. SDL_memcpy(repeat_info->text, text, sizeof(repeat_info->text));
  255. } else {
  256. repeat_info->text[0] = '\0';
  257. }
  258. }
  259. static uint32_t keyboard_repeat_get_key(SDL_WaylandKeyboardRepeat *repeat_info)
  260. {
  261. if (repeat_info->is_initialized && repeat_info->is_key_down) {
  262. return repeat_info->key;
  263. }
  264. return 0;
  265. }
  266. static void keyboard_repeat_set_text(SDL_WaylandKeyboardRepeat *repeat_info, const char text[8])
  267. {
  268. if (repeat_info->is_initialized) {
  269. SDL_copyp(repeat_info->text, text);
  270. }
  271. }
  272. static bool keyboard_repeat_is_set(SDL_WaylandKeyboardRepeat *repeat_info)
  273. {
  274. return repeat_info->is_initialized && repeat_info->is_key_down;
  275. }
  276. static bool keyboard_repeat_key_is_set(SDL_WaylandKeyboardRepeat *repeat_info, uint32_t key)
  277. {
  278. return repeat_info->is_initialized && repeat_info->is_key_down && key == repeat_info->key;
  279. }
  280. static void sync_done_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
  281. {
  282. // Nothing to do, just destroy the callback
  283. wl_callback_destroy(callback);
  284. }
  285. static struct wl_callback_listener sync_listener = {
  286. sync_done_handler
  287. };
  288. void Wayland_SendWakeupEvent(SDL_VideoDevice *_this, SDL_Window *window)
  289. {
  290. SDL_VideoData *d = _this->internal;
  291. /* Queue a sync event to unblock the event queue fd if it's empty and being waited on.
  292. * TODO: Maybe use a pipe to avoid the compositor roundtrip?
  293. */
  294. struct wl_callback *cb = wl_display_sync(d->display);
  295. wl_callback_add_listener(cb, &sync_listener, NULL);
  296. WAYLAND_wl_display_flush(d->display);
  297. }
  298. static int dispatch_queued_events(SDL_VideoData *viddata)
  299. {
  300. int rc;
  301. /*
  302. * NOTE: When reconnection is implemented, check if libdecor needs to be
  303. * involved in the reconnection process.
  304. */
  305. #ifdef HAVE_LIBDECOR_H
  306. if (viddata->shell.libdecor) {
  307. libdecor_dispatch(viddata->shell.libdecor, 0);
  308. }
  309. #endif
  310. rc = WAYLAND_wl_display_dispatch_pending(viddata->display);
  311. return rc >= 0 ? 1 : rc;
  312. }
  313. int Wayland_WaitEventTimeout(SDL_VideoDevice *_this, Sint64 timeoutNS)
  314. {
  315. SDL_VideoData *d = _this->internal;
  316. struct SDL_WaylandInput *input = d->input;
  317. bool key_repeat_active = false;
  318. WAYLAND_wl_display_flush(d->display);
  319. #ifdef SDL_USE_IME
  320. SDL_Window *keyboard_focus = SDL_GetKeyboardFocus();
  321. if (!d->text_input_manager && keyboard_focus && SDL_TextInputActive(keyboard_focus)) {
  322. SDL_IME_PumpEvents();
  323. }
  324. #endif
  325. #ifdef SDL_USE_LIBDBUS
  326. SDL_DBus_PumpEvents();
  327. #endif
  328. // If key repeat is active, we'll need to cap our maximum wait time to handle repeats
  329. if (input && keyboard_repeat_is_set(&input->keyboard_repeat)) {
  330. const Uint64 elapsed = SDL_GetTicksNS() - input->keyboard_repeat.sdl_press_time_ns;
  331. if (keyboard_repeat_handle(&input->keyboard_repeat, elapsed)) {
  332. // A repeat key event was already due
  333. return 1;
  334. } else {
  335. const Uint64 next_repeat_wait_time = (input->keyboard_repeat.next_repeat_ns - elapsed) + 1;
  336. if (timeoutNS >= 0) {
  337. timeoutNS = SDL_min(timeoutNS, next_repeat_wait_time);
  338. } else {
  339. timeoutNS = next_repeat_wait_time;
  340. }
  341. key_repeat_active = true;
  342. }
  343. }
  344. /* wl_display_prepare_read() will return -1 if the default queue is not empty.
  345. * If the default queue is empty, it will prepare us for our SDL_IOReady() call. */
  346. if (WAYLAND_wl_display_prepare_read(d->display) == 0) {
  347. // Use SDL_IOR_NO_RETRY to ensure SIGINT will break us out of our wait
  348. int err = SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_IOR_READ | SDL_IOR_NO_RETRY, timeoutNS);
  349. if (err > 0) {
  350. // There are new events available to read
  351. WAYLAND_wl_display_read_events(d->display);
  352. return dispatch_queued_events(d);
  353. } else if (err == 0) {
  354. // No events available within the timeout
  355. WAYLAND_wl_display_cancel_read(d->display);
  356. // If key repeat is active, we might have woken up to generate a key event
  357. if (key_repeat_active) {
  358. const Uint64 elapsed = SDL_GetTicksNS() - input->keyboard_repeat.sdl_press_time_ns;
  359. if (keyboard_repeat_handle(&input->keyboard_repeat, elapsed)) {
  360. return 1;
  361. }
  362. }
  363. return 0;
  364. } else {
  365. // Error returned from poll()/select()
  366. WAYLAND_wl_display_cancel_read(d->display);
  367. if (errno == EINTR) {
  368. /* If the wait was interrupted by a signal, we may have generated a
  369. * SDL_EVENT_QUIT event. Let the caller know to call SDL_PumpEvents(). */
  370. return 1;
  371. } else {
  372. return err;
  373. }
  374. }
  375. } else {
  376. // We already had pending events
  377. return dispatch_queued_events(d);
  378. }
  379. }
  380. void Wayland_PumpEvents(SDL_VideoDevice *_this)
  381. {
  382. SDL_VideoData *d = _this->internal;
  383. struct SDL_WaylandInput *input = d->input;
  384. int err;
  385. #ifdef SDL_USE_IME
  386. SDL_Window *keyboard_focus = SDL_GetKeyboardFocus();
  387. if (!d->text_input_manager && keyboard_focus && SDL_TextInputActive(keyboard_focus)) {
  388. SDL_IME_PumpEvents();
  389. }
  390. #endif
  391. #ifdef SDL_USE_LIBDBUS
  392. SDL_DBus_PumpEvents();
  393. #endif
  394. #ifdef HAVE_LIBDECOR_H
  395. if (d->shell.libdecor) {
  396. libdecor_dispatch(d->shell.libdecor, 0);
  397. }
  398. #endif
  399. WAYLAND_wl_display_flush(d->display);
  400. /* wl_display_prepare_read() will return -1 if the default queue is not empty.
  401. * If the default queue is empty, it will prepare us for our SDL_IOReady() call. */
  402. if (WAYLAND_wl_display_prepare_read(d->display) == 0) {
  403. if (SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_IOR_READ, 0) > 0) {
  404. WAYLAND_wl_display_read_events(d->display);
  405. } else {
  406. WAYLAND_wl_display_cancel_read(d->display);
  407. }
  408. }
  409. // Dispatch any pre-existing pending events or new events we may have read
  410. err = WAYLAND_wl_display_dispatch_pending(d->display);
  411. if (input && keyboard_repeat_is_set(&input->keyboard_repeat)) {
  412. const Uint64 elapsed = SDL_GetTicksNS() - input->keyboard_repeat.sdl_press_time_ns;
  413. keyboard_repeat_handle(&input->keyboard_repeat, elapsed);
  414. }
  415. if (err < 0 && !d->display_disconnected) {
  416. /* Something has failed with the Wayland connection -- for example,
  417. * the compositor may have shut down and closed its end of the socket,
  418. * or there is a library-specific error.
  419. *
  420. * Try to recover once, then quit.
  421. */
  422. if (!Wayland_VideoReconnect(_this)) {
  423. d->display_disconnected = 1;
  424. SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Wayland display connection closed by server (fatal)");
  425. /* Only send a single quit message, as application shutdown might call
  426. * SDL_PumpEvents
  427. */
  428. SDL_SendQuit();
  429. }
  430. }
  431. }
  432. static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
  433. uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
  434. {
  435. struct SDL_WaylandInput *input = data;
  436. SDL_WindowData *window_data = input->pointer_focus;
  437. SDL_Window *window = window_data ? window_data->sdlwindow : NULL;
  438. input->sx_w = sx_w;
  439. input->sy_w = sy_w;
  440. if (input->pointer_focus) {
  441. const float sx = (float)(wl_fixed_to_double(sx_w) * window_data->pointer_scale.x);
  442. const float sy = (float)(wl_fixed_to_double(sy_w) * window_data->pointer_scale.y);
  443. SDL_SendMouseMotion(Wayland_GetPointerTimestamp(input, time), window_data->sdlwindow, input->pointer_id, false, sx, sy);
  444. }
  445. if (window && window->hit_test) {
  446. const SDL_Point point = { (int)SDL_floor(wl_fixed_to_double(sx_w) * window_data->pointer_scale.x),
  447. (int)SDL_floor(wl_fixed_to_double(sy_w) * window_data->pointer_scale.y) };
  448. SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
  449. if (rc == window_data->hit_test_result) {
  450. return;
  451. }
  452. Wayland_SetHitTestCursor(rc);
  453. window_data->hit_test_result = rc;
  454. }
  455. }
  456. static void pointer_handle_enter(void *data, struct wl_pointer *pointer,
  457. uint32_t serial, struct wl_surface *surface,
  458. wl_fixed_t sx_w, wl_fixed_t sy_w)
  459. {
  460. struct SDL_WaylandInput *input = data;
  461. SDL_WindowData *window;
  462. if (!surface) {
  463. // enter event for a window we've just destroyed
  464. return;
  465. }
  466. /* This handler will be called twice in Wayland 1.4
  467. * Once for the window surface which has valid user data
  468. * and again for the mouse cursor surface which does not have valid user data
  469. * We ignore the later
  470. */
  471. window = Wayland_GetWindowDataForOwnedSurface(surface);
  472. if (window) {
  473. input->pointer_focus = window;
  474. input->pointer_enter_serial = serial;
  475. SDL_SetMouseFocus(window->sdlwindow);
  476. /* In the case of e.g. a pointer confine warp, we may receive an enter
  477. * event with no following motion event, but with the new coordinates
  478. * as part of the enter event.
  479. *
  480. * FIXME: This causes a movement event with an anomalous timestamp when
  481. * the cursor enters the window.
  482. */
  483. pointer_handle_motion(data, pointer, 0, sx_w, sy_w);
  484. /* If the cursor was changed while our window didn't have pointer
  485. * focus, we might need to trigger another call to
  486. * wl_pointer_set_cursor() for the new cursor to be displayed. */
  487. Wayland_SetHitTestCursor(window->hit_test_result);
  488. }
  489. }
  490. static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
  491. uint32_t serial, struct wl_surface *surface)
  492. {
  493. struct SDL_WaylandInput *input = data;
  494. if (!surface) {
  495. return;
  496. }
  497. if (input->pointer_focus) {
  498. SDL_WindowData *wind = Wayland_GetWindowDataForOwnedSurface(surface);
  499. if (wind) {
  500. // Clear the capture flag and raise all buttons
  501. wind->sdlwindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
  502. input->buttons_pressed = 0;
  503. SDL_SendMouseButton(0, wind->sdlwindow, input->pointer_id, SDL_BUTTON_LEFT, false);
  504. SDL_SendMouseButton(0, wind->sdlwindow, input->pointer_id, SDL_BUTTON_RIGHT, false);
  505. SDL_SendMouseButton(0, wind->sdlwindow, input->pointer_id, SDL_BUTTON_MIDDLE, false);
  506. SDL_SendMouseButton(0, wind->sdlwindow, input->pointer_id, SDL_BUTTON_X1, false);
  507. SDL_SendMouseButton(0, wind->sdlwindow, input->pointer_id, SDL_BUTTON_X2, false);
  508. }
  509. /* A pointer leave event may be emitted if the compositor hides the pointer in response to receiving a touch event.
  510. * Don't relinquish focus if the surface has active touches, as the compositor is just transitioning from mouse to touch mode.
  511. */
  512. if (!Wayland_SurfaceHasActiveTouches(surface)) {
  513. SDL_SetMouseFocus(NULL);
  514. }
  515. input->pointer_focus = NULL;
  516. }
  517. }
  518. static bool ProcessHitTest(SDL_WindowData *window_data,
  519. struct wl_seat *seat,
  520. wl_fixed_t sx_w, wl_fixed_t sy_w,
  521. uint32_t serial)
  522. {
  523. SDL_Window *window = window_data->sdlwindow;
  524. if (window->hit_test) {
  525. static const uint32_t directions[] = {
  526. XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT, XDG_TOPLEVEL_RESIZE_EDGE_TOP,
  527. XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT, XDG_TOPLEVEL_RESIZE_EDGE_RIGHT,
  528. XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT, XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM,
  529. XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT, XDG_TOPLEVEL_RESIZE_EDGE_LEFT
  530. };
  531. #ifdef HAVE_LIBDECOR_H
  532. static const uint32_t directions_libdecor[] = {
  533. LIBDECOR_RESIZE_EDGE_TOP_LEFT, LIBDECOR_RESIZE_EDGE_TOP,
  534. LIBDECOR_RESIZE_EDGE_TOP_RIGHT, LIBDECOR_RESIZE_EDGE_RIGHT,
  535. LIBDECOR_RESIZE_EDGE_BOTTOM_RIGHT, LIBDECOR_RESIZE_EDGE_BOTTOM,
  536. LIBDECOR_RESIZE_EDGE_BOTTOM_LEFT, LIBDECOR_RESIZE_EDGE_LEFT
  537. };
  538. #endif
  539. switch (window_data->hit_test_result) {
  540. case SDL_HITTEST_DRAGGABLE:
  541. #ifdef HAVE_LIBDECOR_H
  542. if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
  543. if (window_data->shell_surface.libdecor.frame) {
  544. libdecor_frame_move(window_data->shell_surface.libdecor.frame,
  545. seat,
  546. serial);
  547. }
  548. } else
  549. #endif
  550. if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL) {
  551. if (window_data->shell_surface.xdg.toplevel.xdg_toplevel) {
  552. xdg_toplevel_move(window_data->shell_surface.xdg.toplevel.xdg_toplevel,
  553. seat,
  554. serial);
  555. }
  556. }
  557. return true;
  558. case SDL_HITTEST_RESIZE_TOPLEFT:
  559. case SDL_HITTEST_RESIZE_TOP:
  560. case SDL_HITTEST_RESIZE_TOPRIGHT:
  561. case SDL_HITTEST_RESIZE_RIGHT:
  562. case SDL_HITTEST_RESIZE_BOTTOMRIGHT:
  563. case SDL_HITTEST_RESIZE_BOTTOM:
  564. case SDL_HITTEST_RESIZE_BOTTOMLEFT:
  565. case SDL_HITTEST_RESIZE_LEFT:
  566. #ifdef HAVE_LIBDECOR_H
  567. if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
  568. if (window_data->shell_surface.libdecor.frame) {
  569. libdecor_frame_resize(window_data->shell_surface.libdecor.frame,
  570. seat,
  571. serial,
  572. directions_libdecor[window_data->hit_test_result - SDL_HITTEST_RESIZE_TOPLEFT]);
  573. }
  574. } else
  575. #endif
  576. if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL) {
  577. if (window_data->shell_surface.xdg.toplevel.xdg_toplevel) {
  578. xdg_toplevel_resize(window_data->shell_surface.xdg.toplevel.xdg_toplevel,
  579. seat,
  580. serial,
  581. directions[window_data->hit_test_result - SDL_HITTEST_RESIZE_TOPLEFT]);
  582. }
  583. }
  584. return true;
  585. default:
  586. return false;
  587. }
  588. }
  589. return false;
  590. }
  591. static void pointer_handle_button_common(struct SDL_WaylandInput *input, uint32_t serial,
  592. uint32_t time, uint32_t button, uint32_t state_w)
  593. {
  594. SDL_WindowData *window = input->pointer_focus;
  595. enum wl_pointer_button_state state = state_w;
  596. Uint64 timestamp = Wayland_GetPointerTimestamp(input, time);
  597. Uint8 sdl_button;
  598. const bool down = (state != 0);
  599. switch (button) {
  600. case BTN_LEFT:
  601. sdl_button = SDL_BUTTON_LEFT;
  602. break;
  603. case BTN_MIDDLE:
  604. sdl_button = SDL_BUTTON_MIDDLE;
  605. break;
  606. case BTN_RIGHT:
  607. sdl_button = SDL_BUTTON_RIGHT;
  608. break;
  609. case BTN_SIDE:
  610. sdl_button = SDL_BUTTON_X1;
  611. break;
  612. case BTN_EXTRA:
  613. sdl_button = SDL_BUTTON_X2;
  614. break;
  615. default:
  616. return;
  617. }
  618. if (window) {
  619. SDL_VideoData *viddata = window->waylandData;
  620. bool ignore_click = false;
  621. if (state) {
  622. Wayland_UpdateImplicitGrabSerial(input, serial);
  623. input->buttons_pressed |= SDL_BUTTON_MASK(sdl_button);
  624. } else {
  625. input->buttons_pressed &= ~(SDL_BUTTON_MASK(sdl_button));
  626. }
  627. if (sdl_button == SDL_BUTTON_LEFT &&
  628. ProcessHitTest(input->pointer_focus, input->seat, input->sx_w, input->sy_w, serial)) {
  629. return; // don't pass this event on to app.
  630. }
  631. // Possibly ignore this click if it was to gain focus.
  632. if (window->last_focus_event_time_ns) {
  633. if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
  634. (SDL_GetTicksNS() - window->last_focus_event_time_ns) < WAYLAND_FOCUS_CLICK_TIMEOUT_NS) {
  635. ignore_click = !SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, false);
  636. }
  637. window->last_focus_event_time_ns = 0;
  638. }
  639. /* Wayland won't let you "capture" the mouse, but it will automatically track
  640. * the mouse outside the window if you drag outside of it, until you let go
  641. * of all buttons (even if you add or remove presses outside the window, as
  642. * long as any button is still down, the capture remains).
  643. *
  644. * The mouse is not captured in relative mode.
  645. */
  646. if (!viddata->relative_mouse_mode) {
  647. if (input->buttons_pressed != 0) {
  648. window->sdlwindow->flags |= SDL_WINDOW_MOUSE_CAPTURE;
  649. } else {
  650. window->sdlwindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
  651. }
  652. }
  653. if (!ignore_click) {
  654. SDL_SendMouseButton(timestamp, window->sdlwindow, input->pointer_id, sdl_button, down);
  655. }
  656. }
  657. }
  658. static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
  659. uint32_t time, uint32_t button, uint32_t state_w)
  660. {
  661. struct SDL_WaylandInput *input = data;
  662. pointer_handle_button_common(input, serial, time, button, state_w);
  663. }
  664. static void pointer_handle_axis_common_v1(struct SDL_WaylandInput *input,
  665. uint32_t time, uint32_t axis, wl_fixed_t value)
  666. {
  667. SDL_WindowData *window = input->pointer_focus;
  668. const Uint64 timestamp = Wayland_GetPointerTimestamp(input, time);
  669. const enum wl_pointer_axis a = axis;
  670. if (input->pointer_focus) {
  671. float x, y;
  672. switch (a) {
  673. case WL_POINTER_AXIS_VERTICAL_SCROLL:
  674. x = 0;
  675. y = 0 - (float)wl_fixed_to_double(value);
  676. break;
  677. case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
  678. x = (float)wl_fixed_to_double(value);
  679. y = 0;
  680. break;
  681. default:
  682. return;
  683. }
  684. x /= WAYLAND_WHEEL_AXIS_UNIT;
  685. y /= WAYLAND_WHEEL_AXIS_UNIT;
  686. SDL_SendMouseWheel(timestamp, window->sdlwindow, input->pointer_id, x, y, SDL_MOUSEWHEEL_NORMAL);
  687. }
  688. }
  689. static void pointer_handle_axis_common(struct SDL_WaylandInput *input, enum SDL_WaylandAxisEvent type,
  690. uint32_t axis, wl_fixed_t value)
  691. {
  692. const enum wl_pointer_axis a = axis;
  693. if (input->pointer_focus) {
  694. switch (a) {
  695. case WL_POINTER_AXIS_VERTICAL_SCROLL:
  696. switch (type) {
  697. case AXIS_EVENT_VALUE120:
  698. /*
  699. * High resolution scroll event. The spec doesn't state that axis_value120
  700. * events are limited to one per frame, so the values are accumulated.
  701. */
  702. if (input->pointer_curr_axis_info.y_axis_type != AXIS_EVENT_VALUE120) {
  703. input->pointer_curr_axis_info.y_axis_type = AXIS_EVENT_VALUE120;
  704. input->pointer_curr_axis_info.y = 0.0f;
  705. }
  706. input->pointer_curr_axis_info.y += 0 - (float)wl_fixed_to_double(value);
  707. break;
  708. case AXIS_EVENT_DISCRETE:
  709. /*
  710. * This is a discrete axis event, so we process it and set the
  711. * flag to ignore future continuous axis events in this frame.
  712. */
  713. if (input->pointer_curr_axis_info.y_axis_type != AXIS_EVENT_DISCRETE) {
  714. input->pointer_curr_axis_info.y_axis_type = AXIS_EVENT_DISCRETE;
  715. input->pointer_curr_axis_info.y = 0 - (float)wl_fixed_to_double(value);
  716. }
  717. break;
  718. case AXIS_EVENT_CONTINUOUS:
  719. // Only process continuous events if no discrete events have been received.
  720. if (input->pointer_curr_axis_info.y_axis_type == AXIS_EVENT_CONTINUOUS) {
  721. input->pointer_curr_axis_info.y = 0 - (float)wl_fixed_to_double(value);
  722. }
  723. break;
  724. }
  725. break;
  726. case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
  727. switch (type) {
  728. case AXIS_EVENT_VALUE120:
  729. /*
  730. * High resolution scroll event. The spec doesn't state that axis_value120
  731. * events are limited to one per frame, so the values are accumulated.
  732. */
  733. if (input->pointer_curr_axis_info.x_axis_type != AXIS_EVENT_VALUE120) {
  734. input->pointer_curr_axis_info.x_axis_type = AXIS_EVENT_VALUE120;
  735. input->pointer_curr_axis_info.x = 0.0f;
  736. }
  737. input->pointer_curr_axis_info.x += (float)wl_fixed_to_double(value);
  738. break;
  739. case AXIS_EVENT_DISCRETE:
  740. /*
  741. * This is a discrete axis event, so we process it and set the
  742. * flag to ignore future continuous axis events in this frame.
  743. */
  744. if (input->pointer_curr_axis_info.x_axis_type != AXIS_EVENT_DISCRETE) {
  745. input->pointer_curr_axis_info.x_axis_type = AXIS_EVENT_DISCRETE;
  746. input->pointer_curr_axis_info.x = (float)wl_fixed_to_double(value);
  747. }
  748. break;
  749. case AXIS_EVENT_CONTINUOUS:
  750. // Only process continuous events if no discrete events have been received.
  751. if (input->pointer_curr_axis_info.x_axis_type == AXIS_EVENT_CONTINUOUS) {
  752. input->pointer_curr_axis_info.x = (float)wl_fixed_to_double(value);
  753. }
  754. break;
  755. }
  756. break;
  757. }
  758. }
  759. }
  760. static void pointer_handle_axis(void *data, struct wl_pointer *pointer,
  761. uint32_t time, uint32_t axis, wl_fixed_t value)
  762. {
  763. struct SDL_WaylandInput *input = data;
  764. if (wl_seat_get_version(input->seat) >= WL_POINTER_FRAME_SINCE_VERSION) {
  765. input->pointer_curr_axis_info.timestamp_ns = Wayland_GetPointerTimestamp(input, time);
  766. pointer_handle_axis_common(input, AXIS_EVENT_CONTINUOUS, axis, value);
  767. } else {
  768. pointer_handle_axis_common_v1(input, time, axis, value);
  769. }
  770. }
  771. static void pointer_handle_axis_relative_direction(void *data, struct wl_pointer *pointer,
  772. uint32_t axis, uint32_t axis_relative_direction)
  773. {
  774. struct SDL_WaylandInput *input = data;
  775. if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL) {
  776. return;
  777. }
  778. switch (axis_relative_direction) {
  779. case WL_POINTER_AXIS_RELATIVE_DIRECTION_IDENTICAL:
  780. input->pointer_curr_axis_info.direction = SDL_MOUSEWHEEL_NORMAL;
  781. break;
  782. case WL_POINTER_AXIS_RELATIVE_DIRECTION_INVERTED:
  783. input->pointer_curr_axis_info.direction = SDL_MOUSEWHEEL_FLIPPED;
  784. break;
  785. }
  786. }
  787. static void pointer_handle_frame(void *data, struct wl_pointer *pointer)
  788. {
  789. struct SDL_WaylandInput *input = data;
  790. SDL_WindowData *window = input->pointer_focus;
  791. float x, y;
  792. SDL_MouseWheelDirection direction = input->pointer_curr_axis_info.direction;
  793. switch (input->pointer_curr_axis_info.x_axis_type) {
  794. case AXIS_EVENT_CONTINUOUS:
  795. x = input->pointer_curr_axis_info.x / WAYLAND_WHEEL_AXIS_UNIT;
  796. break;
  797. case AXIS_EVENT_DISCRETE:
  798. x = input->pointer_curr_axis_info.x;
  799. break;
  800. case AXIS_EVENT_VALUE120:
  801. x = input->pointer_curr_axis_info.x / 120.0f;
  802. break;
  803. default:
  804. x = 0.0f;
  805. break;
  806. }
  807. switch (input->pointer_curr_axis_info.y_axis_type) {
  808. case AXIS_EVENT_CONTINUOUS:
  809. y = input->pointer_curr_axis_info.y / WAYLAND_WHEEL_AXIS_UNIT;
  810. break;
  811. case AXIS_EVENT_DISCRETE:
  812. y = input->pointer_curr_axis_info.y;
  813. break;
  814. case AXIS_EVENT_VALUE120:
  815. y = input->pointer_curr_axis_info.y / 120.0f;
  816. break;
  817. default:
  818. y = 0.0f;
  819. break;
  820. }
  821. // clear pointer_curr_axis_info for next frame
  822. SDL_memset(&input->pointer_curr_axis_info, 0, sizeof(input->pointer_curr_axis_info));
  823. if (x != 0.0f || y != 0.0f) {
  824. SDL_SendMouseWheel(input->pointer_curr_axis_info.timestamp_ns,
  825. window->sdlwindow, input->pointer_id, x, y, direction);
  826. }
  827. }
  828. static void pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
  829. uint32_t axis_source)
  830. {
  831. // unimplemented
  832. }
  833. static void pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
  834. uint32_t time, uint32_t axis)
  835. {
  836. // unimplemented
  837. }
  838. static void pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
  839. uint32_t axis, int32_t discrete)
  840. {
  841. struct SDL_WaylandInput *input = data;
  842. pointer_handle_axis_common(input, AXIS_EVENT_DISCRETE, axis, wl_fixed_from_int(discrete));
  843. }
  844. static void pointer_handle_axis_value120(void *data, struct wl_pointer *pointer,
  845. uint32_t axis, int32_t value120)
  846. {
  847. struct SDL_WaylandInput *input = data;
  848. pointer_handle_axis_common(input, AXIS_EVENT_VALUE120, axis, wl_fixed_from_int(value120));
  849. }
  850. static const struct wl_pointer_listener pointer_listener = {
  851. pointer_handle_enter,
  852. pointer_handle_leave,
  853. pointer_handle_motion,
  854. pointer_handle_button,
  855. pointer_handle_axis,
  856. pointer_handle_frame, // Version 5
  857. pointer_handle_axis_source, // Version 5
  858. pointer_handle_axis_stop, // Version 5
  859. pointer_handle_axis_discrete, // Version 5
  860. pointer_handle_axis_value120, // Version 8
  861. pointer_handle_axis_relative_direction // Version 9
  862. };
  863. static void relative_pointer_handle_relative_motion(void *data,
  864. struct zwp_relative_pointer_v1 *pointer,
  865. uint32_t time_hi,
  866. uint32_t time_lo,
  867. wl_fixed_t dx_w,
  868. wl_fixed_t dy_w,
  869. wl_fixed_t dx_unaccel_w,
  870. wl_fixed_t dy_unaccel_w)
  871. {
  872. struct SDL_WaylandInput *input = data;
  873. SDL_VideoData *d = input->display;
  874. SDL_WindowData *window = input->pointer_focus;
  875. // Relative pointer event times are in microsecond granularity.
  876. const Uint64 timestamp = Wayland_GetEventTimestamp(SDL_US_TO_NS(((Uint64)time_hi << 32) | (Uint64)time_lo));
  877. if (input->pointer_focus && d->relative_mouse_mode) {
  878. double dx;
  879. double dy;
  880. if (!SDL_GetMouse()->enable_relative_system_scale) {
  881. dx = wl_fixed_to_double(dx_unaccel_w);
  882. dy = wl_fixed_to_double(dy_unaccel_w);
  883. } else {
  884. dx = wl_fixed_to_double(dx_w) * window->pointer_scale.x;
  885. dy = wl_fixed_to_double(dy_w) * window->pointer_scale.y;
  886. }
  887. SDL_SendMouseMotion(timestamp, window->sdlwindow, input->pointer_id, true, (float)dx, (float)dy);
  888. }
  889. }
  890. static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
  891. relative_pointer_handle_relative_motion,
  892. };
  893. static void locked_pointer_locked(void *data,
  894. struct zwp_locked_pointer_v1 *locked_pointer)
  895. {
  896. }
  897. static void locked_pointer_unlocked(void *data,
  898. struct zwp_locked_pointer_v1 *locked_pointer)
  899. {
  900. }
  901. static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
  902. locked_pointer_locked,
  903. locked_pointer_unlocked,
  904. };
  905. bool Wayland_input_lock_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
  906. {
  907. SDL_WindowData *w = window->internal;
  908. SDL_VideoData *d = input->display;
  909. if (!d->pointer_constraints || !input->pointer) {
  910. return false;
  911. }
  912. if (!w->locked_pointer) {
  913. if (w->confined_pointer) {
  914. // If the pointer is already confined to the surface, the lock will fail with a protocol error.
  915. Wayland_input_unconfine_pointer(input, window);
  916. }
  917. w->locked_pointer = zwp_pointer_constraints_v1_lock_pointer(d->pointer_constraints,
  918. w->surface,
  919. input->pointer,
  920. NULL,
  921. ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
  922. zwp_locked_pointer_v1_add_listener(w->locked_pointer,
  923. &locked_pointer_listener,
  924. window);
  925. }
  926. return true;
  927. }
  928. bool Wayland_input_unlock_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
  929. {
  930. SDL_WindowData *w = window->internal;
  931. if (w->locked_pointer) {
  932. zwp_locked_pointer_v1_destroy(w->locked_pointer);
  933. w->locked_pointer = NULL;
  934. }
  935. // Restore existing pointer confinement.
  936. Wayland_input_confine_pointer(input, window);
  937. return true;
  938. }
  939. static void pointer_confine_destroy(SDL_Window *window)
  940. {
  941. SDL_WindowData *w = window->internal;
  942. if (w->confined_pointer) {
  943. zwp_confined_pointer_v1_destroy(w->confined_pointer);
  944. w->confined_pointer = NULL;
  945. }
  946. }
  947. static void confined_pointer_confined(void *data,
  948. struct zwp_confined_pointer_v1 *confined_pointer)
  949. {
  950. }
  951. static void confined_pointer_unconfined(void *data,
  952. struct zwp_confined_pointer_v1 *confined_pointer)
  953. {
  954. }
  955. static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
  956. confined_pointer_confined,
  957. confined_pointer_unconfined,
  958. };
  959. static void touch_handler_down(void *data, struct wl_touch *touch, uint32_t serial,
  960. uint32_t timestamp, struct wl_surface *surface,
  961. int id, wl_fixed_t fx, wl_fixed_t fy)
  962. {
  963. struct SDL_WaylandInput *input = (struct SDL_WaylandInput *)data;
  964. SDL_WindowData *window_data;
  965. // Check that this surface is valid.
  966. if (!surface) {
  967. return;
  968. }
  969. touch_add(id, fx, fy, surface);
  970. Wayland_UpdateImplicitGrabSerial(input, serial);
  971. window_data = Wayland_GetWindowDataForOwnedSurface(surface);
  972. if (window_data) {
  973. float x, y;
  974. if (window_data->current.logical_width <= 1) {
  975. x = 0.5f;
  976. } else {
  977. x = (float)wl_fixed_to_double(fx) / (window_data->current.logical_width - 1);
  978. }
  979. if (window_data->current.logical_height <= 1) {
  980. y = 0.5f;
  981. } else {
  982. y = (float)wl_fixed_to_double(fy) / (window_data->current.logical_height - 1);
  983. }
  984. SDL_SetMouseFocus(window_data->sdlwindow);
  985. SDL_SendTouch(Wayland_GetTouchTimestamp(input, timestamp), (SDL_TouchID)(uintptr_t)touch,
  986. (SDL_FingerID)(id + 1), window_data->sdlwindow, SDL_EVENT_FINGER_DOWN, x, y, 1.0f);
  987. }
  988. }
  989. static void touch_handler_up(void *data, struct wl_touch *touch, uint32_t serial,
  990. uint32_t timestamp, int id)
  991. {
  992. struct SDL_WaylandInput *input = (struct SDL_WaylandInput *)data;
  993. wl_fixed_t fx = 0, fy = 0;
  994. struct wl_surface *surface = NULL;
  995. touch_del(id, &fx, &fy, &surface);
  996. if (surface) {
  997. SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
  998. if (window_data) {
  999. const float x = (float)wl_fixed_to_double(fx) / window_data->current.logical_width;
  1000. const float y = (float)wl_fixed_to_double(fy) / window_data->current.logical_height;
  1001. SDL_SendTouch(Wayland_GetTouchTimestamp(input, timestamp), (SDL_TouchID)(uintptr_t)touch,
  1002. (SDL_FingerID)(id + 1), window_data->sdlwindow, SDL_EVENT_FINGER_UP, x, y, 0.0f);
  1003. /* If the seat lacks pointer focus, the seat's keyboard focus is another window or NULL, this window currently
  1004. * has mouse focus, and the surface has no active touch events, consider mouse focus to be lost.
  1005. */
  1006. if (!input->pointer_focus && input->keyboard_focus != window_data &&
  1007. SDL_GetMouseFocus() == window_data->sdlwindow && !Wayland_SurfaceHasActiveTouches(surface)) {
  1008. SDL_SetMouseFocus(NULL);
  1009. }
  1010. }
  1011. }
  1012. }
  1013. static void touch_handler_motion(void *data, struct wl_touch *touch, uint32_t timestamp,
  1014. int id, wl_fixed_t fx, wl_fixed_t fy)
  1015. {
  1016. struct SDL_WaylandInput *input = (struct SDL_WaylandInput *)data;
  1017. struct wl_surface *surface = NULL;
  1018. touch_update(id, fx, fy, &surface);
  1019. if (surface) {
  1020. SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
  1021. if (window_data) {
  1022. const float x = (float)wl_fixed_to_double(fx) / window_data->current.logical_width;
  1023. const float y = (float)wl_fixed_to_double(fy) / window_data->current.logical_height;
  1024. SDL_SendTouchMotion(Wayland_GetTouchTimestamp(input, timestamp), (SDL_TouchID)(uintptr_t)touch,
  1025. (SDL_FingerID)(id + 1), window_data->sdlwindow, x, y, 1.0f);
  1026. }
  1027. }
  1028. }
  1029. static void touch_handler_frame(void *data, struct wl_touch *touch)
  1030. {
  1031. }
  1032. static void touch_handler_cancel(void *data, struct wl_touch *touch)
  1033. {
  1034. struct SDL_WaylandInput *input = (struct SDL_WaylandInput *)data;
  1035. struct SDL_WaylandTouchPoint *tp, *temp;
  1036. wl_list_for_each_safe (tp, temp, &touch_points, link) {
  1037. bool removed = false;
  1038. if (tp->surface) {
  1039. SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(tp->surface);
  1040. if (window_data) {
  1041. const float x = (float)(wl_fixed_to_double(tp->fx) / window_data->current.logical_width);
  1042. const float y = (float)(wl_fixed_to_double(tp->fy) / window_data->current.logical_height);
  1043. SDL_SendTouch(0, (SDL_TouchID)(uintptr_t)touch,
  1044. (SDL_FingerID)(tp->id + 1), window_data->sdlwindow, SDL_EVENT_FINGER_CANCELED, x, y, 0.0f);
  1045. // Remove the touch from the list before checking for still-active touches on the surface.
  1046. WAYLAND_wl_list_remove(&tp->link);
  1047. removed = true;
  1048. /* If the seat lacks pointer focus, the seat's keyboard focus is another window or NULL, this window currently
  1049. * has mouse focus, and the surface has no active touch events, consider mouse focus to be lost.
  1050. */
  1051. if (!input->pointer_focus && input->keyboard_focus != window_data &&
  1052. SDL_GetMouseFocus() == window_data->sdlwindow && !Wayland_SurfaceHasActiveTouches(tp->surface)) {
  1053. SDL_SetMouseFocus(NULL);
  1054. }
  1055. }
  1056. }
  1057. if (!removed) {
  1058. WAYLAND_wl_list_remove(&tp->link);
  1059. }
  1060. SDL_free(tp);
  1061. }
  1062. }
  1063. static const struct wl_touch_listener touch_listener = {
  1064. touch_handler_down,
  1065. touch_handler_up,
  1066. touch_handler_motion,
  1067. touch_handler_frame,
  1068. touch_handler_cancel,
  1069. NULL, // shape
  1070. NULL, // orientation
  1071. };
  1072. typedef struct Wayland_Keymap
  1073. {
  1074. SDL_Keymap *keymap;
  1075. struct xkb_state *state;
  1076. SDL_Keymod modstate;
  1077. } Wayland_Keymap;
  1078. static void Wayland_keymap_iter(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
  1079. {
  1080. Wayland_Keymap *sdlKeymap = (Wayland_Keymap *)data;
  1081. const xkb_keysym_t *syms;
  1082. const SDL_Scancode scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, (key - 8));
  1083. if (scancode == SDL_SCANCODE_UNKNOWN) {
  1084. return;
  1085. }
  1086. if (WAYLAND_xkb_state_key_get_syms(sdlKeymap->state, key, &syms) > 0) {
  1087. SDL_Keycode keycode = SDL_GetKeyCodeFromKeySym(syms[0], key, sdlKeymap->modstate);
  1088. if (!keycode) {
  1089. switch (scancode) {
  1090. case SDL_SCANCODE_RETURN:
  1091. keycode = SDLK_RETURN;
  1092. break;
  1093. case SDL_SCANCODE_ESCAPE:
  1094. keycode = SDLK_ESCAPE;
  1095. break;
  1096. case SDL_SCANCODE_BACKSPACE:
  1097. keycode = SDLK_BACKSPACE;
  1098. break;
  1099. case SDL_SCANCODE_DELETE:
  1100. keycode = SDLK_DELETE;
  1101. break;
  1102. default:
  1103. keycode = SDL_SCANCODE_TO_KEYCODE(scancode);
  1104. break;
  1105. }
  1106. }
  1107. SDL_SetKeymapEntry(sdlKeymap->keymap, scancode, sdlKeymap->modstate, keycode);
  1108. }
  1109. }
  1110. static void Wayland_UpdateKeymap(struct SDL_WaylandInput *input)
  1111. {
  1112. struct Keymod_masks
  1113. {
  1114. SDL_Keymod sdl_mask;
  1115. xkb_mod_mask_t xkb_mask;
  1116. } const keymod_masks[] = {
  1117. { SDL_KMOD_NONE, 0 },
  1118. { SDL_KMOD_SHIFT, input->xkb.idx_shift },
  1119. { SDL_KMOD_CAPS, input->xkb.idx_caps },
  1120. { SDL_KMOD_SHIFT | SDL_KMOD_CAPS, input->xkb.idx_shift | input->xkb.idx_caps },
  1121. { SDL_KMOD_MODE, input->xkb.idx_mod5 },
  1122. { SDL_KMOD_MODE | SDL_KMOD_SHIFT, input->xkb.idx_mod5 | input->xkb.idx_shift },
  1123. { SDL_KMOD_MODE | SDL_KMOD_CAPS, input->xkb.idx_mod5 | input->xkb.idx_caps },
  1124. { SDL_KMOD_MODE | SDL_KMOD_SHIFT | SDL_KMOD_CAPS, input->xkb.idx_mod5 | input->xkb.idx_shift | input->xkb.idx_caps },
  1125. { SDL_KMOD_LEVEL5, input->xkb.idx_mod3 },
  1126. { SDL_KMOD_LEVEL5 | SDL_KMOD_SHIFT, input->xkb.idx_mod3 | input->xkb.idx_shift },
  1127. { SDL_KMOD_LEVEL5 | SDL_KMOD_CAPS, input->xkb.idx_mod3 | input->xkb.idx_caps },
  1128. { SDL_KMOD_LEVEL5 | SDL_KMOD_SHIFT | SDL_KMOD_CAPS, input->xkb.idx_mod3 | input->xkb.idx_shift | input->xkb.idx_caps },
  1129. { SDL_KMOD_LEVEL5 | SDL_KMOD_MODE, input->xkb.idx_mod3 | input->xkb.idx_mod5 },
  1130. { SDL_KMOD_LEVEL5 | SDL_KMOD_MODE | SDL_KMOD_SHIFT, input->xkb.idx_mod3 | input->xkb.idx_mod5 | input->xkb.idx_shift },
  1131. { SDL_KMOD_LEVEL5 | SDL_KMOD_MODE | SDL_KMOD_CAPS, input->xkb.idx_mod3 | input->xkb.idx_mod5 | input->xkb.idx_caps },
  1132. { SDL_KMOD_LEVEL5 | SDL_KMOD_MODE | SDL_KMOD_SHIFT | SDL_KMOD_CAPS, input->xkb.idx_mod3 | input->xkb.idx_mod5 | input->xkb.idx_shift | input->xkb.idx_caps },
  1133. };
  1134. if (!input->keyboard_is_virtual) {
  1135. Wayland_Keymap keymap;
  1136. keymap.keymap = SDL_CreateKeymap();
  1137. if (!keymap.keymap) {
  1138. return;
  1139. }
  1140. keymap.state = WAYLAND_xkb_state_new(input->xkb.keymap);
  1141. if (!keymap.state) {
  1142. SDL_SetError("failed to create XKB state");
  1143. SDL_DestroyKeymap(keymap.keymap);
  1144. return;
  1145. }
  1146. for (int i = 0; i < SDL_arraysize(keymod_masks); ++i) {
  1147. keymap.modstate = keymod_masks[i].sdl_mask;
  1148. WAYLAND_xkb_state_update_mask(keymap.state,
  1149. keymod_masks[i].xkb_mask & (input->xkb.idx_shift | input->xkb.idx_mod5 | input->xkb.idx_mod3), 0, keymod_masks[i].xkb_mask & input->xkb.idx_caps,
  1150. 0, 0, input->xkb.current_group);
  1151. WAYLAND_xkb_keymap_key_for_each(input->xkb.keymap,
  1152. Wayland_keymap_iter,
  1153. &keymap);
  1154. }
  1155. WAYLAND_xkb_state_unref(keymap.state);
  1156. SDL_SetKeymap(keymap.keymap, true);
  1157. } else {
  1158. // Virtual keyboards use the default keymap.
  1159. SDL_SetKeymap(NULL, true);
  1160. }
  1161. }
  1162. static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
  1163. uint32_t format, int fd, uint32_t size)
  1164. {
  1165. struct SDL_WaylandInput *input = data;
  1166. char *map_str;
  1167. const char *locale;
  1168. if (!data) {
  1169. close(fd);
  1170. return;
  1171. }
  1172. if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
  1173. close(fd);
  1174. return;
  1175. }
  1176. map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
  1177. if (map_str == MAP_FAILED) {
  1178. close(fd);
  1179. return;
  1180. }
  1181. if (input->xkb.keymap != NULL) {
  1182. /* if there's already a keymap loaded, throw it away rather than leaking it before
  1183. * parsing the new one
  1184. */
  1185. WAYLAND_xkb_keymap_unref(input->xkb.keymap);
  1186. input->xkb.keymap = NULL;
  1187. }
  1188. input->xkb.keymap = WAYLAND_xkb_keymap_new_from_string(input->display->xkb_context,
  1189. map_str,
  1190. XKB_KEYMAP_FORMAT_TEXT_V1,
  1191. 0);
  1192. munmap(map_str, size);
  1193. close(fd);
  1194. if (!input->xkb.keymap) {
  1195. SDL_SetError("failed to compile keymap");
  1196. return;
  1197. }
  1198. #define GET_MOD_INDEX(mod) \
  1199. WAYLAND_xkb_keymap_mod_get_index(input->xkb.keymap, XKB_MOD_NAME_##mod)
  1200. input->xkb.idx_shift = 1 << GET_MOD_INDEX(SHIFT);
  1201. input->xkb.idx_ctrl = 1 << GET_MOD_INDEX(CTRL);
  1202. input->xkb.idx_alt = 1 << GET_MOD_INDEX(ALT);
  1203. input->xkb.idx_gui = 1 << GET_MOD_INDEX(LOGO);
  1204. input->xkb.idx_mod3 = 1 << GET_MOD_INDEX(MOD3);
  1205. input->xkb.idx_mod5 = 1 << GET_MOD_INDEX(MOD5);
  1206. input->xkb.idx_num = 1 << GET_MOD_INDEX(NUM);
  1207. input->xkb.idx_caps = 1 << GET_MOD_INDEX(CAPS);
  1208. #undef GET_MOD_INDEX
  1209. if (input->xkb.state != NULL) {
  1210. /* if there's already a state, throw it away rather than leaking it before
  1211. * trying to create a new one with the new keymap.
  1212. */
  1213. WAYLAND_xkb_state_unref(input->xkb.state);
  1214. input->xkb.state = NULL;
  1215. }
  1216. input->xkb.state = WAYLAND_xkb_state_new(input->xkb.keymap);
  1217. if (!input->xkb.state) {
  1218. SDL_SetError("failed to create XKB state");
  1219. WAYLAND_xkb_keymap_unref(input->xkb.keymap);
  1220. input->xkb.keymap = NULL;
  1221. return;
  1222. }
  1223. /*
  1224. * Assume that a nameless layout implies a virtual keyboard with an arbitrary layout.
  1225. * TODO: Use a better method of detection?
  1226. */
  1227. input->keyboard_is_virtual = WAYLAND_xkb_keymap_layout_get_name(input->xkb.keymap, 0) == NULL;
  1228. // Update the keymap if changed.
  1229. if (input->xkb.current_group != XKB_GROUP_INVALID) {
  1230. Wayland_UpdateKeymap(input);
  1231. }
  1232. /*
  1233. * See https://blogs.s-osg.org/compose-key-support-weston/
  1234. * for further explanation on dead keys in Wayland.
  1235. */
  1236. // Look up the preferred locale, falling back to "C" as default
  1237. locale = SDL_getenv("LC_ALL");
  1238. if (!locale) {
  1239. locale = SDL_getenv("LC_CTYPE");
  1240. if (!locale) {
  1241. locale = SDL_getenv("LANG");
  1242. if (!locale) {
  1243. locale = "C";
  1244. }
  1245. }
  1246. }
  1247. // Set up XKB compose table
  1248. if (input->xkb.compose_table != NULL) {
  1249. WAYLAND_xkb_compose_table_unref(input->xkb.compose_table);
  1250. input->xkb.compose_table = NULL;
  1251. }
  1252. input->xkb.compose_table = WAYLAND_xkb_compose_table_new_from_locale(input->display->xkb_context,
  1253. locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
  1254. if (input->xkb.compose_table) {
  1255. // Set up XKB compose state
  1256. if (input->xkb.compose_state != NULL) {
  1257. WAYLAND_xkb_compose_state_unref(input->xkb.compose_state);
  1258. input->xkb.compose_state = NULL;
  1259. }
  1260. input->xkb.compose_state = WAYLAND_xkb_compose_state_new(input->xkb.compose_table,
  1261. XKB_COMPOSE_STATE_NO_FLAGS);
  1262. if (!input->xkb.compose_state) {
  1263. SDL_SetError("could not create XKB compose state");
  1264. WAYLAND_xkb_compose_table_unref(input->xkb.compose_table);
  1265. input->xkb.compose_table = NULL;
  1266. }
  1267. }
  1268. }
  1269. /*
  1270. * Virtual keyboards can have arbitrary layouts, arbitrary scancodes/keycodes, etc...
  1271. * Key presses from these devices must be looked up by their keysym value.
  1272. */
  1273. static SDL_Scancode Wayland_GetScancodeForKey(struct SDL_WaylandInput *input, uint32_t key)
  1274. {
  1275. SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
  1276. if (!input->keyboard_is_virtual) {
  1277. scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, key);
  1278. } else {
  1279. const xkb_keysym_t *syms;
  1280. if (WAYLAND_xkb_keymap_key_get_syms_by_level(input->xkb.keymap, key + 8, input->xkb.current_group, 0, &syms) > 0) {
  1281. scancode = SDL_GetScancodeFromKeySym(syms[0], key);
  1282. }
  1283. }
  1284. return scancode;
  1285. }
  1286. static void Wayland_ReconcileModifiers(struct SDL_WaylandInput *input, bool key_pressed)
  1287. {
  1288. /* Handle explicit pressed modifier state. This will correct the modifier state
  1289. * if common modifier keys were remapped and the modifiers presumed to be set
  1290. * during a key press event were incorrect, or if the modifier was set to the
  1291. * pressed state via means other than pressing the physical key.
  1292. */
  1293. if (!key_pressed) {
  1294. if (input->xkb.wl_pressed_modifiers & input->xkb.idx_shift) {
  1295. if (!(input->pressed_modifiers & SDL_KMOD_SHIFT)) {
  1296. input->pressed_modifiers |= SDL_KMOD_SHIFT;
  1297. }
  1298. } else {
  1299. input->pressed_modifiers &= ~SDL_KMOD_SHIFT;
  1300. }
  1301. if (input->xkb.wl_pressed_modifiers & input->xkb.idx_ctrl) {
  1302. if (!(input->pressed_modifiers & SDL_KMOD_CTRL)) {
  1303. input->pressed_modifiers |= SDL_KMOD_CTRL;
  1304. }
  1305. } else {
  1306. input->pressed_modifiers &= ~SDL_KMOD_CTRL;
  1307. }
  1308. if (input->xkb.wl_pressed_modifiers & input->xkb.idx_alt) {
  1309. if (!(input->pressed_modifiers & SDL_KMOD_ALT)) {
  1310. input->pressed_modifiers |= SDL_KMOD_ALT;
  1311. }
  1312. } else {
  1313. input->pressed_modifiers &= ~SDL_KMOD_ALT;
  1314. }
  1315. if (input->xkb.wl_pressed_modifiers & input->xkb.idx_gui) {
  1316. if (!(input->pressed_modifiers & SDL_KMOD_GUI)) {
  1317. input->pressed_modifiers |= SDL_KMOD_GUI;
  1318. }
  1319. } else {
  1320. input->pressed_modifiers &= ~SDL_KMOD_GUI;
  1321. }
  1322. /* Note: This is not backwards: in the default keymap, Mod5 is typically
  1323. * level 3 shift, and Mod3 is typically level 5 shift.
  1324. */
  1325. if (input->xkb.wl_pressed_modifiers & input->xkb.idx_mod3) {
  1326. if (!(input->pressed_modifiers & SDL_KMOD_LEVEL5)) {
  1327. input->pressed_modifiers |= SDL_KMOD_LEVEL5;
  1328. }
  1329. } else {
  1330. input->pressed_modifiers &= ~SDL_KMOD_LEVEL5;
  1331. }
  1332. if (input->xkb.wl_pressed_modifiers & input->xkb.idx_mod5) {
  1333. if (!(input->pressed_modifiers & SDL_KMOD_MODE)) {
  1334. input->pressed_modifiers |= SDL_KMOD_MODE;
  1335. }
  1336. } else {
  1337. input->pressed_modifiers &= ~SDL_KMOD_MODE;
  1338. }
  1339. }
  1340. /* If a latch or lock was activated by a keypress, the latch/lock will
  1341. * be tied to the specific left/right key that initiated it. Otherwise,
  1342. * the ambiguous left/right combo is used.
  1343. *
  1344. * The modifier will remain active until the latch/lock is released by
  1345. * the system.
  1346. */
  1347. if (input->xkb.wl_locked_modifiers & input->xkb.idx_shift) {
  1348. if (input->pressed_modifiers & SDL_KMOD_SHIFT) {
  1349. input->locked_modifiers &= ~SDL_KMOD_SHIFT;
  1350. input->locked_modifiers |= (input->pressed_modifiers & SDL_KMOD_SHIFT);
  1351. } else if (!(input->locked_modifiers & SDL_KMOD_SHIFT)) {
  1352. input->locked_modifiers |= SDL_KMOD_SHIFT;
  1353. }
  1354. } else {
  1355. input->locked_modifiers &= ~SDL_KMOD_SHIFT;
  1356. }
  1357. if (input->xkb.wl_locked_modifiers & input->xkb.idx_ctrl) {
  1358. if (input->pressed_modifiers & SDL_KMOD_CTRL) {
  1359. input->locked_modifiers &= ~SDL_KMOD_CTRL;
  1360. input->locked_modifiers |= (input->pressed_modifiers & SDL_KMOD_CTRL);
  1361. } else if (!(input->locked_modifiers & SDL_KMOD_CTRL)) {
  1362. input->locked_modifiers |= SDL_KMOD_CTRL;
  1363. }
  1364. } else {
  1365. input->locked_modifiers &= ~SDL_KMOD_CTRL;
  1366. }
  1367. if (input->xkb.wl_locked_modifiers & input->xkb.idx_alt) {
  1368. if (input->pressed_modifiers & SDL_KMOD_ALT) {
  1369. input->locked_modifiers &= ~SDL_KMOD_ALT;
  1370. input->locked_modifiers |= (input->pressed_modifiers & SDL_KMOD_ALT);
  1371. } else if (!(input->locked_modifiers & SDL_KMOD_ALT)) {
  1372. input->locked_modifiers |= SDL_KMOD_ALT;
  1373. }
  1374. } else {
  1375. input->locked_modifiers &= ~SDL_KMOD_ALT;
  1376. }
  1377. if (input->xkb.wl_locked_modifiers & input->xkb.idx_gui) {
  1378. if (input->pressed_modifiers & SDL_KMOD_GUI) {
  1379. input->locked_modifiers &= ~SDL_KMOD_GUI;
  1380. input->locked_modifiers |= (input->pressed_modifiers & SDL_KMOD_GUI);
  1381. } else if (!(input->locked_modifiers & SDL_KMOD_GUI)) {
  1382. input->locked_modifiers |= SDL_KMOD_GUI;
  1383. }
  1384. } else {
  1385. input->locked_modifiers &= ~SDL_KMOD_GUI;
  1386. }
  1387. // As above, this is correct: Mod3 is typically level 5 shift, and Mod5 is typically level 3 shift.
  1388. if (input->xkb.wl_locked_modifiers & input->xkb.idx_mod3) {
  1389. input->locked_modifiers |= SDL_KMOD_LEVEL5;
  1390. } else {
  1391. input->locked_modifiers &= ~SDL_KMOD_LEVEL5;
  1392. }
  1393. if (input->xkb.wl_locked_modifiers & input->xkb.idx_mod5) {
  1394. input->locked_modifiers |= SDL_KMOD_MODE;
  1395. } else {
  1396. input->locked_modifiers &= ~SDL_KMOD_MODE;
  1397. }
  1398. // Capslock and Numlock can only be locked, not pressed.
  1399. if (input->xkb.wl_locked_modifiers & input->xkb.idx_caps) {
  1400. input->locked_modifiers |= SDL_KMOD_CAPS;
  1401. } else {
  1402. input->locked_modifiers &= ~SDL_KMOD_CAPS;
  1403. }
  1404. if (input->xkb.wl_locked_modifiers & input->xkb.idx_num) {
  1405. input->locked_modifiers |= SDL_KMOD_NUM;
  1406. } else {
  1407. input->locked_modifiers &= ~SDL_KMOD_NUM;
  1408. }
  1409. SDL_SetModState(input->pressed_modifiers | input->locked_modifiers);
  1410. }
  1411. static void Wayland_HandleModifierKeys(struct SDL_WaylandInput *input, SDL_Scancode scancode, bool pressed)
  1412. {
  1413. const SDL_Keycode keycode = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false);
  1414. SDL_Keymod mod;
  1415. /* SDL clients expect modifier state to be activated at the same time as the
  1416. * source keypress, so we set pressed modifier state with the usual modifier
  1417. * keys here, as the explicit modifier event won't arrive until after the
  1418. * keypress event. If this is wrong, it will be corrected when the explicit
  1419. * modifier state is sent at a later time.
  1420. */
  1421. switch (keycode) {
  1422. case SDLK_LSHIFT:
  1423. mod = SDL_KMOD_LSHIFT;
  1424. break;
  1425. case SDLK_RSHIFT:
  1426. mod = SDL_KMOD_RSHIFT;
  1427. break;
  1428. case SDLK_LCTRL:
  1429. mod = SDL_KMOD_LCTRL;
  1430. break;
  1431. case SDLK_RCTRL:
  1432. mod = SDL_KMOD_RCTRL;
  1433. break;
  1434. case SDLK_LALT:
  1435. mod = SDL_KMOD_LALT;
  1436. break;
  1437. case SDLK_RALT:
  1438. mod = SDL_KMOD_RALT;
  1439. break;
  1440. case SDLK_LGUI:
  1441. mod = SDL_KMOD_LGUI;
  1442. break;
  1443. case SDLK_RGUI:
  1444. mod = SDL_KMOD_RGUI;
  1445. break;
  1446. case SDLK_MODE:
  1447. mod = SDL_KMOD_MODE;
  1448. break;
  1449. case SDLK_LEVEL5_SHIFT:
  1450. mod = SDL_KMOD_LEVEL5;
  1451. break;
  1452. default:
  1453. return;
  1454. }
  1455. if (pressed) {
  1456. input->pressed_modifiers |= mod;
  1457. } else {
  1458. input->pressed_modifiers &= ~mod;
  1459. }
  1460. Wayland_ReconcileModifiers(input, true);
  1461. }
  1462. static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
  1463. uint32_t serial, struct wl_surface *surface,
  1464. struct wl_array *keys)
  1465. {
  1466. struct SDL_WaylandInput *input = data;
  1467. SDL_WindowData *window;
  1468. uint32_t *key;
  1469. if (!surface) {
  1470. // enter event for a window we've just destroyed
  1471. return;
  1472. }
  1473. window = Wayland_GetWindowDataForOwnedSurface(surface);
  1474. if (!window) {
  1475. return;
  1476. }
  1477. input->keyboard_focus = window;
  1478. window->keyboard_device = input;
  1479. // Restore the keyboard focus to the child popup that was holding it
  1480. SDL_SetKeyboardFocus(window->sdlwindow->keyboard_focus ? window->sdlwindow->keyboard_focus : window->sdlwindow);
  1481. #ifdef SDL_USE_IME
  1482. if (!input->text_input) {
  1483. SDL_IME_SetFocus(true);
  1484. }
  1485. #endif
  1486. Uint64 timestamp = SDL_GetTicksNS();
  1487. window->last_focus_event_time_ns = timestamp;
  1488. wl_array_for_each (key, keys) {
  1489. const SDL_Scancode scancode = Wayland_GetScancodeForKey(input, *key);
  1490. const SDL_Keycode keycode = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false);
  1491. switch (keycode) {
  1492. case SDLK_LSHIFT:
  1493. case SDLK_RSHIFT:
  1494. case SDLK_LCTRL:
  1495. case SDLK_RCTRL:
  1496. case SDLK_LALT:
  1497. case SDLK_RALT:
  1498. case SDLK_LGUI:
  1499. case SDLK_RGUI:
  1500. case SDLK_MODE:
  1501. case SDLK_LEVEL5_SHIFT:
  1502. Wayland_HandleModifierKeys(input, scancode, true);
  1503. SDL_SendKeyboardKeyIgnoreModifiers(timestamp, input->keyboard_id, *key, scancode, true);
  1504. break;
  1505. default:
  1506. break;
  1507. }
  1508. }
  1509. }
  1510. static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
  1511. uint32_t serial, struct wl_surface *surface)
  1512. {
  1513. struct SDL_WaylandInput *input = data;
  1514. SDL_WindowData *wind;
  1515. SDL_Window *window = NULL;
  1516. if (!surface) {
  1517. return;
  1518. }
  1519. wind = Wayland_GetWindowDataForOwnedSurface(surface);
  1520. if (!wind) {
  1521. return;
  1522. }
  1523. wind->keyboard_device = NULL;
  1524. window = wind->sdlwindow;
  1525. // Stop key repeat before clearing keyboard focus
  1526. keyboard_repeat_clear(&input->keyboard_repeat);
  1527. // This will release any keys still pressed
  1528. SDL_SetKeyboardFocus(NULL);
  1529. input->keyboard_focus = NULL;
  1530. // Clear the pressed modifiers.
  1531. input->pressed_modifiers = SDL_KMOD_NONE;
  1532. #ifdef SDL_USE_IME
  1533. if (!input->text_input) {
  1534. SDL_IME_SetFocus(false);
  1535. }
  1536. #endif
  1537. /* If the surface had a pointer leave event while still having active touch events, it retained mouse focus.
  1538. * Clear it now if all touch events are raised.
  1539. */
  1540. if (!input->pointer_focus && SDL_GetMouseFocus() == window && !Wayland_SurfaceHasActiveTouches(surface)) {
  1541. SDL_SetMouseFocus(NULL);
  1542. }
  1543. }
  1544. static bool keyboard_input_get_text(char text[8], const struct SDL_WaylandInput *input, uint32_t key, bool down, bool *handled_by_ime)
  1545. {
  1546. SDL_WindowData *window = input->keyboard_focus;
  1547. const xkb_keysym_t *syms;
  1548. xkb_keysym_t sym;
  1549. if (!window || window->keyboard_device != input || !input->xkb.state) {
  1550. return false;
  1551. }
  1552. // TODO: Can this happen?
  1553. if (WAYLAND_xkb_state_key_get_syms(input->xkb.state, key + 8, &syms) != 1) {
  1554. return false;
  1555. }
  1556. sym = syms[0];
  1557. #ifdef SDL_USE_IME
  1558. if (SDL_IME_ProcessKeyEvent(sym, key + 8, down)) {
  1559. if (handled_by_ime) {
  1560. *handled_by_ime = true;
  1561. }
  1562. return true;
  1563. }
  1564. #endif
  1565. if (!down) {
  1566. return false;
  1567. }
  1568. if (input->xkb.compose_state && WAYLAND_xkb_compose_state_feed(input->xkb.compose_state, sym) == XKB_COMPOSE_FEED_ACCEPTED) {
  1569. switch (WAYLAND_xkb_compose_state_get_status(input->xkb.compose_state)) {
  1570. case XKB_COMPOSE_COMPOSING:
  1571. if (handled_by_ime) {
  1572. *handled_by_ime = true;
  1573. }
  1574. return true;
  1575. case XKB_COMPOSE_CANCELLED:
  1576. default:
  1577. sym = XKB_KEY_NoSymbol;
  1578. break;
  1579. case XKB_COMPOSE_NOTHING:
  1580. break;
  1581. case XKB_COMPOSE_COMPOSED:
  1582. sym = WAYLAND_xkb_compose_state_get_one_sym(input->xkb.compose_state);
  1583. break;
  1584. }
  1585. }
  1586. return WAYLAND_xkb_keysym_to_utf8(sym, text, 8) > 0;
  1587. }
  1588. static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
  1589. uint32_t serial, uint32_t time, uint32_t key,
  1590. uint32_t state_w)
  1591. {
  1592. struct SDL_WaylandInput *input = data;
  1593. enum wl_keyboard_key_state state = state_w;
  1594. char text[8];
  1595. bool has_text = false;
  1596. bool handled_by_ime = false;
  1597. const Uint64 timestamp_ns = Wayland_GetKeyboardTimestamp(input, time);
  1598. Wayland_UpdateImplicitGrabSerial(input, serial);
  1599. if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
  1600. SDL_Window *keyboard_focus = SDL_GetKeyboardFocus();
  1601. if (keyboard_focus && SDL_TextInputActive(keyboard_focus)) {
  1602. has_text = keyboard_input_get_text(text, input, key, true, &handled_by_ime);
  1603. }
  1604. } else {
  1605. if (keyboard_repeat_key_is_set(&input->keyboard_repeat, key)) {
  1606. /* Send any due key repeat events before stopping the repeat and generating the key up event.
  1607. * Compute time based on the Wayland time, as it reports when the release event happened.
  1608. * Using SDL_GetTicks would be wrong, as it would report when the release event is processed,
  1609. * which may be off if the application hasn't pumped events for a while.
  1610. */
  1611. const Uint64 elapsed = SDL_MS_TO_NS(time - input->keyboard_repeat.wl_press_time_ms);
  1612. keyboard_repeat_handle(&input->keyboard_repeat, elapsed);
  1613. keyboard_repeat_clear(&input->keyboard_repeat);
  1614. }
  1615. keyboard_input_get_text(text, input, key, false, &handled_by_ime);
  1616. }
  1617. const SDL_Scancode scancode = Wayland_GetScancodeForKey(input, key);
  1618. Wayland_HandleModifierKeys(input, scancode, state == WL_KEYBOARD_KEY_STATE_PRESSED);
  1619. SDL_SendKeyboardKeyIgnoreModifiers(timestamp_ns, input->keyboard_id, key, scancode, state == WL_KEYBOARD_KEY_STATE_PRESSED);
  1620. if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
  1621. if (has_text && !(SDL_GetModState() & (SDL_KMOD_CTRL | SDL_KMOD_ALT))) {
  1622. if (!handled_by_ime) {
  1623. SDL_SendKeyboardText(text);
  1624. }
  1625. }
  1626. if (input->xkb.keymap && WAYLAND_xkb_keymap_key_repeats(input->xkb.keymap, key + 8)) {
  1627. keyboard_repeat_set(&input->keyboard_repeat, input->keyboard_id, key, time, timestamp_ns, scancode, has_text, text);
  1628. }
  1629. }
  1630. }
  1631. static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
  1632. uint32_t serial, uint32_t mods_depressed,
  1633. uint32_t mods_latched, uint32_t mods_locked,
  1634. uint32_t group)
  1635. {
  1636. struct SDL_WaylandInput *input = data;
  1637. if (input->xkb.state == NULL) {
  1638. /* if we get a modifier notification before the keymap, there's nothing we can do with the information
  1639. */
  1640. return;
  1641. }
  1642. WAYLAND_xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
  1643. mods_locked, 0, 0, group);
  1644. input->xkb.wl_pressed_modifiers = mods_depressed;
  1645. input->xkb.wl_locked_modifiers = mods_latched | mods_locked;
  1646. Wayland_ReconcileModifiers(input, false);
  1647. // If a key is repeating, update the text to apply the modifier.
  1648. if (keyboard_repeat_is_set(&input->keyboard_repeat)) {
  1649. char text[8];
  1650. const uint32_t key = keyboard_repeat_get_key(&input->keyboard_repeat);
  1651. if (keyboard_input_get_text(text, input, key, true, NULL)) {
  1652. keyboard_repeat_set_text(&input->keyboard_repeat, text);
  1653. }
  1654. }
  1655. if (group == input->xkb.current_group) {
  1656. return;
  1657. }
  1658. // The layout changed, remap and fire an event. Virtual keyboards use the default keymap.
  1659. input->xkb.current_group = group;
  1660. Wayland_UpdateKeymap(input);
  1661. }
  1662. static void keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
  1663. int32_t rate, int32_t delay)
  1664. {
  1665. struct SDL_WaylandInput *input = data;
  1666. input->keyboard_repeat.repeat_rate = SDL_clamp(rate, 0, 1000);
  1667. input->keyboard_repeat.repeat_delay_ms = delay;
  1668. input->keyboard_repeat.is_initialized = true;
  1669. }
  1670. static const struct wl_keyboard_listener keyboard_listener = {
  1671. keyboard_handle_keymap,
  1672. keyboard_handle_enter,
  1673. keyboard_handle_leave,
  1674. keyboard_handle_key,
  1675. keyboard_handle_modifiers,
  1676. keyboard_handle_repeat_info, // Version 4
  1677. };
  1678. void Wayland_input_init_relative_pointer(SDL_VideoData *d)
  1679. {
  1680. struct SDL_WaylandInput *input = d->input;
  1681. if (!d->relative_pointer_manager) {
  1682. return;
  1683. }
  1684. if (input->pointer && !input->relative_pointer) {
  1685. input->relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(input->display->relative_pointer_manager, input->pointer);
  1686. zwp_relative_pointer_v1_add_listener(input->relative_pointer,
  1687. &relative_pointer_listener,
  1688. input);
  1689. }
  1690. }
  1691. static void seat_handle_capabilities(void *data, struct wl_seat *seat,
  1692. enum wl_seat_capability caps)
  1693. {
  1694. struct SDL_WaylandInput *input = data;
  1695. if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
  1696. input->pointer = wl_seat_get_pointer(seat);
  1697. SDL_memset(&input->pointer_curr_axis_info, 0, sizeof(input->pointer_curr_axis_info));
  1698. input->display->pointer = input->pointer;
  1699. Wayland_CreateCursorShapeDevice(input);
  1700. wl_pointer_set_user_data(input->pointer, input);
  1701. wl_pointer_add_listener(input->pointer, &pointer_listener, input);
  1702. Wayland_input_init_relative_pointer(input->display);
  1703. input->pointer_id = SDL_GetNextObjectID();
  1704. SDL_AddMouse(input->pointer_id, WAYLAND_DEFAULT_POINTER_NAME, !input->display->initializing);
  1705. } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
  1706. if (input->relative_pointer) {
  1707. zwp_relative_pointer_v1_destroy(input->relative_pointer);
  1708. input->relative_pointer = NULL;
  1709. }
  1710. if (input->cursor_shape) {
  1711. wp_cursor_shape_device_v1_destroy(input->cursor_shape);
  1712. input->cursor_shape = NULL;
  1713. }
  1714. if (wl_pointer_get_version(input->pointer) >= WL_POINTER_RELEASE_SINCE_VERSION) {
  1715. wl_pointer_release(input->pointer);
  1716. } else {
  1717. wl_pointer_destroy(input->pointer);
  1718. }
  1719. input->pointer = NULL;
  1720. input->display->pointer = NULL;
  1721. SDL_RemoveMouse(input->pointer_id, true);
  1722. input->pointer_id = 0;
  1723. }
  1724. if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
  1725. input->touch = wl_seat_get_touch(seat);
  1726. SDL_AddTouch((SDL_TouchID)(uintptr_t)input->touch, SDL_TOUCH_DEVICE_DIRECT, "wayland_touch");
  1727. wl_touch_set_user_data(input->touch, input);
  1728. wl_touch_add_listener(input->touch, &touch_listener,
  1729. input);
  1730. } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
  1731. SDL_DelTouch((SDL_TouchID)(intptr_t)input->touch);
  1732. wl_touch_destroy(input->touch);
  1733. input->touch = NULL;
  1734. }
  1735. if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
  1736. input->keyboard = wl_seat_get_keyboard(seat);
  1737. wl_keyboard_set_user_data(input->keyboard, input);
  1738. wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
  1739. input);
  1740. input->keyboard_id = SDL_GetNextObjectID();
  1741. SDL_AddKeyboard(input->keyboard_id, WAYLAND_DEFAULT_KEYBOARD_NAME, !input->display->initializing);
  1742. } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
  1743. wl_keyboard_destroy(input->keyboard);
  1744. input->keyboard = NULL;
  1745. SDL_RemoveKeyboard(input->keyboard_id, true);
  1746. input->keyboard_id = 0;
  1747. }
  1748. Wayland_RegisterTimestampListeners(input);
  1749. }
  1750. static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
  1751. {
  1752. // unimplemented
  1753. }
  1754. static const struct wl_seat_listener seat_listener = {
  1755. seat_handle_capabilities,
  1756. seat_handle_name, // Version 2
  1757. };
  1758. static void data_source_handle_target(void *data, struct wl_data_source *wl_data_source,
  1759. const char *mime_type)
  1760. {
  1761. }
  1762. static void data_source_handle_send(void *data, struct wl_data_source *wl_data_source,
  1763. const char *mime_type, int32_t fd)
  1764. {
  1765. Wayland_data_source_send((SDL_WaylandDataSource *)data, mime_type, fd);
  1766. }
  1767. static void data_source_handle_cancelled(void *data, struct wl_data_source *wl_data_source)
  1768. {
  1769. SDL_WaylandDataSource *source = data;
  1770. if (source) {
  1771. Wayland_data_source_destroy(source);
  1772. }
  1773. }
  1774. static void data_source_handle_dnd_drop_performed(void *data, struct wl_data_source *wl_data_source)
  1775. {
  1776. }
  1777. static void data_source_handle_dnd_finished(void *data, struct wl_data_source *wl_data_source)
  1778. {
  1779. }
  1780. static void data_source_handle_action(void *data, struct wl_data_source *wl_data_source,
  1781. uint32_t dnd_action)
  1782. {
  1783. }
  1784. static const struct wl_data_source_listener data_source_listener = {
  1785. data_source_handle_target,
  1786. data_source_handle_send,
  1787. data_source_handle_cancelled,
  1788. data_source_handle_dnd_drop_performed, // Version 3
  1789. data_source_handle_dnd_finished, // Version 3
  1790. data_source_handle_action, // Version 3
  1791. };
  1792. static void primary_selection_source_send(void *data, struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1,
  1793. const char *mime_type, int32_t fd)
  1794. {
  1795. Wayland_primary_selection_source_send((SDL_WaylandPrimarySelectionSource *)data,
  1796. mime_type, fd);
  1797. }
  1798. static void primary_selection_source_cancelled(void *data, struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1)
  1799. {
  1800. Wayland_primary_selection_source_destroy(data);
  1801. }
  1802. static const struct zwp_primary_selection_source_v1_listener primary_selection_source_listener = {
  1803. primary_selection_source_send,
  1804. primary_selection_source_cancelled,
  1805. };
  1806. SDL_WaylandDataSource *Wayland_data_source_create(SDL_VideoDevice *_this)
  1807. {
  1808. SDL_WaylandDataSource *data_source = NULL;
  1809. SDL_VideoData *driver_data = NULL;
  1810. struct wl_data_source *id = NULL;
  1811. if (!_this || !_this->internal) {
  1812. SDL_SetError("Video driver uninitialized");
  1813. } else {
  1814. driver_data = _this->internal;
  1815. if (driver_data->data_device_manager) {
  1816. id = wl_data_device_manager_create_data_source(
  1817. driver_data->data_device_manager);
  1818. }
  1819. if (!id) {
  1820. SDL_SetError("Wayland unable to create data source");
  1821. } else {
  1822. data_source = SDL_calloc(1, sizeof(*data_source));
  1823. if (!data_source) {
  1824. wl_data_source_destroy(id);
  1825. } else {
  1826. data_source->source = id;
  1827. wl_data_source_set_user_data(id, data_source);
  1828. wl_data_source_add_listener(id, &data_source_listener,
  1829. data_source);
  1830. }
  1831. }
  1832. }
  1833. return data_source;
  1834. }
  1835. SDL_WaylandPrimarySelectionSource *Wayland_primary_selection_source_create(SDL_VideoDevice *_this)
  1836. {
  1837. SDL_WaylandPrimarySelectionSource *primary_selection_source = NULL;
  1838. SDL_VideoData *driver_data = NULL;
  1839. struct zwp_primary_selection_source_v1 *id = NULL;
  1840. if (!_this || !_this->internal) {
  1841. SDL_SetError("Video driver uninitialized");
  1842. } else {
  1843. driver_data = _this->internal;
  1844. if (driver_data->primary_selection_device_manager) {
  1845. id = zwp_primary_selection_device_manager_v1_create_source(
  1846. driver_data->primary_selection_device_manager);
  1847. }
  1848. if (!id) {
  1849. SDL_SetError("Wayland unable to create primary selection source");
  1850. } else {
  1851. primary_selection_source = SDL_calloc(1, sizeof(*primary_selection_source));
  1852. if (!primary_selection_source) {
  1853. zwp_primary_selection_source_v1_destroy(id);
  1854. } else {
  1855. primary_selection_source->source = id;
  1856. zwp_primary_selection_source_v1_add_listener(id, &primary_selection_source_listener,
  1857. primary_selection_source);
  1858. }
  1859. }
  1860. }
  1861. return primary_selection_source;
  1862. }
  1863. static void data_offer_handle_offer(void *data, struct wl_data_offer *wl_data_offer,
  1864. const char *mime_type)
  1865. {
  1866. SDL_WaylandDataOffer *offer = data;
  1867. Wayland_data_offer_add_mime(offer, mime_type);
  1868. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1869. ". In wl_data_offer_listener . data_offer_handle_offer on data_offer 0x%08x for MIME '%s'",
  1870. (wl_data_offer ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)wl_data_offer) : -1),
  1871. mime_type);
  1872. }
  1873. static void data_offer_handle_source_actions(void *data, struct wl_data_offer *wl_data_offer,
  1874. uint32_t source_actions)
  1875. {
  1876. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1877. ". In wl_data_offer_listener . data_offer_handle_source_actions on data_offer 0x%08x for Source Actions '%d'",
  1878. (wl_data_offer ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)wl_data_offer) : -1),
  1879. source_actions);
  1880. }
  1881. static void data_offer_handle_actions(void *data, struct wl_data_offer *wl_data_offer,
  1882. uint32_t dnd_action)
  1883. {
  1884. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1885. ". In wl_data_offer_listener . data_offer_handle_actions on data_offer 0x%08x for DND Actions '%d'",
  1886. (wl_data_offer ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)wl_data_offer) : -1),
  1887. dnd_action);
  1888. }
  1889. static const struct wl_data_offer_listener data_offer_listener = {
  1890. data_offer_handle_offer,
  1891. data_offer_handle_source_actions, // Version 3
  1892. data_offer_handle_actions, // Version 3
  1893. };
  1894. static void primary_selection_offer_handle_offer(void *data, struct zwp_primary_selection_offer_v1 *zwp_primary_selection_offer_v1,
  1895. const char *mime_type)
  1896. {
  1897. SDL_WaylandPrimarySelectionOffer *offer = data;
  1898. Wayland_primary_selection_offer_add_mime(offer, mime_type);
  1899. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1900. ". In zwp_primary_selection_offer_v1_listener . primary_selection_offer_handle_offer on primary_selection_offer 0x%08x for MIME '%s'",
  1901. (zwp_primary_selection_offer_v1 ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)zwp_primary_selection_offer_v1) : -1),
  1902. mime_type);
  1903. }
  1904. static const struct zwp_primary_selection_offer_v1_listener primary_selection_offer_listener = {
  1905. primary_selection_offer_handle_offer,
  1906. };
  1907. static void data_device_handle_data_offer(void *data, struct wl_data_device *wl_data_device,
  1908. struct wl_data_offer *id)
  1909. {
  1910. SDL_WaylandDataOffer *data_offer = SDL_calloc(1, sizeof(*data_offer));
  1911. if (data_offer) {
  1912. data_offer->offer = id;
  1913. data_offer->data_device = data;
  1914. WAYLAND_wl_list_init(&(data_offer->mimes));
  1915. wl_data_offer_set_user_data(id, data_offer);
  1916. wl_data_offer_add_listener(id, &data_offer_listener, data_offer);
  1917. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1918. ". In wl_data_device_listener . data_device_handle_data_offer on data_offer 0x%08x",
  1919. (id ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)id) : -1));
  1920. }
  1921. }
  1922. static void data_device_handle_enter(void *data, struct wl_data_device *wl_data_device,
  1923. uint32_t serial, struct wl_surface *surface,
  1924. wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *id)
  1925. {
  1926. SDL_WaylandDataDevice *data_device = data;
  1927. data_device->has_mime_file = false;
  1928. data_device->has_mime_text = false;
  1929. uint32_t dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
  1930. data_device->drag_serial = serial;
  1931. if (id) {
  1932. data_device->drag_offer = wl_data_offer_get_user_data(id);
  1933. // TODO: SDL Support more mime types
  1934. #ifdef SDL_USE_LIBDBUS
  1935. if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_PORTAL_MIME)) {
  1936. data_device->has_mime_file = true;
  1937. wl_data_offer_accept(id, serial, FILE_PORTAL_MIME);
  1938. }
  1939. #endif
  1940. if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_MIME)) {
  1941. data_device->has_mime_file = true;
  1942. wl_data_offer_accept(id, serial, FILE_MIME);
  1943. }
  1944. if (Wayland_data_offer_has_mime(data_device->drag_offer, TEXT_MIME)) {
  1945. data_device->has_mime_text = true;
  1946. wl_data_offer_accept(id, serial, TEXT_MIME);
  1947. }
  1948. // SDL only supports "copy" style drag and drop
  1949. if (data_device->has_mime_file || data_device->has_mime_text) {
  1950. dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
  1951. } else {
  1952. // drag_mime is NULL this will decline the offer
  1953. wl_data_offer_accept(id, serial, NULL);
  1954. }
  1955. if (wl_data_offer_get_version(data_device->drag_offer->offer) >=
  1956. WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
  1957. wl_data_offer_set_actions(data_device->drag_offer->offer,
  1958. dnd_action, dnd_action);
  1959. }
  1960. // find the current window
  1961. if (surface) {
  1962. SDL_WindowData *window = Wayland_GetWindowDataForOwnedSurface(surface);
  1963. if (window) {
  1964. data_device->dnd_window = window->sdlwindow;
  1965. const float dx = (float)wl_fixed_to_double(x);
  1966. const float dy = (float)wl_fixed_to_double(y);
  1967. SDL_SendDropPosition(data_device->dnd_window, dx, dy);
  1968. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1969. ". In wl_data_device_listener . data_device_handle_enter on data_offer 0x%08x at %d x %d into window %d for serial %d",
  1970. WAYLAND_wl_proxy_get_id((struct wl_proxy *)id),
  1971. wl_fixed_to_int(x), wl_fixed_to_int(y), SDL_GetWindowID(data_device->dnd_window), serial);
  1972. } else {
  1973. data_device->dnd_window = NULL;
  1974. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1975. ". In wl_data_device_listener . data_device_handle_enter on data_offer 0x%08x at %d x %d for serial %d",
  1976. WAYLAND_wl_proxy_get_id((struct wl_proxy *)id),
  1977. wl_fixed_to_int(x), wl_fixed_to_int(y), serial);
  1978. }
  1979. } else {
  1980. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1981. ". In wl_data_device_listener . data_device_handle_enter on data_offer 0x%08x at %d x %d for serial %d",
  1982. WAYLAND_wl_proxy_get_id((struct wl_proxy *)id),
  1983. wl_fixed_to_int(x), wl_fixed_to_int(y), serial);
  1984. }
  1985. } else {
  1986. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1987. ". In wl_data_device_listener . data_device_handle_enter on data_offer 0x%08x at %d x %d for serial %d",
  1988. -1, wl_fixed_to_int(x), wl_fixed_to_int(y), serial);
  1989. }
  1990. }
  1991. static void data_device_handle_leave(void *data, struct wl_data_device *wl_data_device)
  1992. {
  1993. SDL_WaylandDataDevice *data_device = data;
  1994. if (data_device->drag_offer) {
  1995. if (data_device->dnd_window) {
  1996. SDL_SendDropComplete(data_device->dnd_window);
  1997. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1998. ". In wl_data_device_listener . data_device_handle_leave on data_offer 0x%08x from window %d for serial %d",
  1999. WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
  2000. SDL_GetWindowID(data_device->dnd_window), data_device->drag_serial);
  2001. } else {
  2002. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  2003. ". In wl_data_device_listener . data_device_handle_leave on data_offer 0x%08x for serial %d",
  2004. WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
  2005. data_device->drag_serial);
  2006. }
  2007. Wayland_data_offer_destroy(data_device->drag_offer);
  2008. data_device->drag_offer = NULL;
  2009. } else {
  2010. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  2011. ". In wl_data_device_listener . data_device_handle_leave on data_offer 0x%08x for serial %d",
  2012. -1, -1);
  2013. }
  2014. data_device->has_mime_file = false;
  2015. data_device->has_mime_text = false;
  2016. }
  2017. static void data_device_handle_motion(void *data, struct wl_data_device *wl_data_device,
  2018. uint32_t time, wl_fixed_t x, wl_fixed_t y)
  2019. {
  2020. SDL_WaylandDataDevice *data_device = data;
  2021. if (data_device->drag_offer && data_device->dnd_window && (data_device->has_mime_file || data_device->has_mime_text)) {
  2022. const float dx = (float)wl_fixed_to_double(x);
  2023. const float dy = (float)wl_fixed_to_double(y);
  2024. /* XXX: Send the filename here if the event system ever starts passing it though.
  2025. * Any future implementation should cache the filenames, as otherwise this could
  2026. * hammer the DBus interface hundreds or even thousands of times per second.
  2027. */
  2028. SDL_SendDropPosition(data_device->dnd_window, dx, dy);
  2029. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  2030. ". In wl_data_device_listener . data_device_handle_motion on data_offer 0x%08x at %d x %d in window %d serial %d",
  2031. WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
  2032. wl_fixed_to_int(x), wl_fixed_to_int(y),
  2033. SDL_GetWindowID(data_device->dnd_window), data_device->drag_serial);
  2034. } else {
  2035. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  2036. ". In wl_data_device_listener . data_device_handle_motion on data_offer 0x%08x at %d x %d serial %d",
  2037. -1, wl_fixed_to_int(x), wl_fixed_to_int(y), -1);
  2038. }
  2039. }
  2040. static void data_device_handle_drop(void *data, struct wl_data_device *wl_data_device)
  2041. {
  2042. SDL_WaylandDataDevice *data_device = data;
  2043. if (data_device->drag_offer && data_device->dnd_window && (data_device->has_mime_file || data_device->has_mime_text)) {
  2044. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  2045. ". In wl_data_device_listener . data_device_handle_drop on data_offer 0x%08x in window %d serial %d",
  2046. WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
  2047. SDL_GetWindowID(data_device->dnd_window), data_device->drag_serial);
  2048. // TODO: SDL Support more mime types
  2049. size_t length;
  2050. bool drop_handled = false;
  2051. #ifdef SDL_USE_LIBDBUS
  2052. if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_PORTAL_MIME)) {
  2053. void *buffer = Wayland_data_offer_receive(data_device->drag_offer,
  2054. FILE_PORTAL_MIME, &length);
  2055. if (buffer) {
  2056. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  2057. if (dbus) {
  2058. int path_count = 0;
  2059. char **paths = SDL_DBus_DocumentsPortalRetrieveFiles(buffer, &path_count);
  2060. // If dropped files contain a directory the list is empty
  2061. if (paths && path_count > 0) {
  2062. int i;
  2063. for (i = 0; i < path_count; i++) {
  2064. SDL_SendDropFile(data_device->dnd_window, NULL, paths[i]);
  2065. }
  2066. dbus->free_string_array(paths);
  2067. SDL_SendDropComplete(data_device->dnd_window);
  2068. drop_handled = true;
  2069. }
  2070. }
  2071. SDL_free(buffer);
  2072. }
  2073. }
  2074. #endif
  2075. /* If XDG document portal fails fallback.
  2076. * When running a flatpak sandbox this will most likely be a list of
  2077. * non paths that are not visible to the application
  2078. */
  2079. if (!drop_handled) {
  2080. const char *mime_type = data_device->has_mime_file ? FILE_MIME : (data_device->has_mime_text ? TEXT_MIME : "");
  2081. void *buffer = Wayland_data_offer_receive(data_device->drag_offer,
  2082. mime_type, &length);
  2083. if (data_device->has_mime_file) {
  2084. if (buffer) {
  2085. char *saveptr = NULL;
  2086. char *token = SDL_strtok_r((char *)buffer, "\r\n", &saveptr);
  2087. while (token) {
  2088. if (SDL_URIToLocal(token, token) >= 0) {
  2089. SDL_SendDropFile(data_device->dnd_window, NULL, token);
  2090. }
  2091. token = SDL_strtok_r(NULL, "\r\n", &saveptr);
  2092. }
  2093. SDL_free(buffer);
  2094. SDL_SendDropComplete(data_device->dnd_window);
  2095. } else {
  2096. SDL_SendDropComplete(data_device->dnd_window);
  2097. }
  2098. drop_handled = true;
  2099. } else if (data_device->has_mime_text) {
  2100. if (buffer) {
  2101. char *saveptr = NULL;
  2102. char *token = SDL_strtok_r((char *)buffer, "\r\n", &saveptr);
  2103. while (token) {
  2104. SDL_SendDropText(data_device->dnd_window, token);
  2105. token = SDL_strtok_r(NULL, "\r\n", &saveptr);
  2106. }
  2107. SDL_free(buffer);
  2108. SDL_SendDropComplete(data_device->dnd_window);
  2109. } else {
  2110. /* Even though there has been a valid data offer,
  2111. * and there have been valid Enter, Motion, and Drop callbacks,
  2112. * Wayland_data_offer_receive may return an empty buffer,
  2113. * because the data is actually in the primary selection device,
  2114. * not in the data device.
  2115. */
  2116. SDL_SendDropComplete(data_device->dnd_window);
  2117. }
  2118. drop_handled = true;
  2119. }
  2120. }
  2121. if (drop_handled && wl_data_offer_get_version(data_device->drag_offer->offer) >= WL_DATA_OFFER_FINISH_SINCE_VERSION) {
  2122. wl_data_offer_finish(data_device->drag_offer->offer);
  2123. }
  2124. } else {
  2125. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  2126. ". In wl_data_device_listener . data_device_handle_drop on data_offer 0x%08x serial %d",
  2127. -1, -1);
  2128. }
  2129. Wayland_data_offer_destroy(data_device->drag_offer);
  2130. data_device->drag_offer = NULL;
  2131. }
  2132. static void notifyFromMimes(struct wl_list *mimes)
  2133. {
  2134. int nformats = 0;
  2135. char **new_mime_types = NULL;
  2136. if (mimes) {
  2137. nformats = WAYLAND_wl_list_length(mimes);
  2138. size_t alloc_size = (nformats + 1) * sizeof(char *);
  2139. /* do a first pass to compute allocation size */
  2140. SDL_MimeDataList *item = NULL;
  2141. wl_list_for_each(item, mimes, link) {
  2142. alloc_size += SDL_strlen(item->mime_type) + 1;
  2143. }
  2144. new_mime_types = SDL_AllocateTemporaryMemory(alloc_size);
  2145. if (!new_mime_types) {
  2146. SDL_LogError(SDL_LOG_CATEGORY_INPUT, "unable to allocate new_mime_types");
  2147. return;
  2148. }
  2149. /* second pass to fill*/
  2150. char *strPtr = (char *)(new_mime_types + nformats + 1);
  2151. item = NULL;
  2152. int i = 0;
  2153. wl_list_for_each(item, mimes, link) {
  2154. new_mime_types[i] = strPtr;
  2155. strPtr = stpcpy(strPtr, item->mime_type) + 1;
  2156. i++;
  2157. }
  2158. new_mime_types[nformats] = NULL;
  2159. }
  2160. SDL_SendClipboardUpdate(false, new_mime_types, nformats);
  2161. }
  2162. static void data_device_handle_selection(void *data, struct wl_data_device *wl_data_device,
  2163. struct wl_data_offer *id)
  2164. {
  2165. SDL_WaylandDataDevice *data_device = data;
  2166. SDL_WaylandDataOffer *offer = NULL;
  2167. if (id) {
  2168. offer = wl_data_offer_get_user_data(id);
  2169. }
  2170. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  2171. ". In data_device_listener . data_device_handle_selection on data_offer 0x%08x",
  2172. (id ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)id) : -1));
  2173. if (data_device->selection_offer != offer) {
  2174. Wayland_data_offer_destroy(data_device->selection_offer);
  2175. data_device->selection_offer = offer;
  2176. }
  2177. notifyFromMimes(offer ? &offer->mimes : NULL);
  2178. }
  2179. static const struct wl_data_device_listener data_device_listener = {
  2180. data_device_handle_data_offer,
  2181. data_device_handle_enter,
  2182. data_device_handle_leave,
  2183. data_device_handle_motion,
  2184. data_device_handle_drop,
  2185. data_device_handle_selection
  2186. };
  2187. static void primary_selection_device_handle_offer(void *data, struct zwp_primary_selection_device_v1 *zwp_primary_selection_device_v1,
  2188. struct zwp_primary_selection_offer_v1 *id)
  2189. {
  2190. SDL_WaylandPrimarySelectionOffer *primary_selection_offer = SDL_calloc(1, sizeof(*primary_selection_offer));
  2191. if (primary_selection_offer) {
  2192. primary_selection_offer->offer = id;
  2193. primary_selection_offer->primary_selection_device = data;
  2194. WAYLAND_wl_list_init(&(primary_selection_offer->mimes));
  2195. zwp_primary_selection_offer_v1_set_user_data(id, primary_selection_offer);
  2196. zwp_primary_selection_offer_v1_add_listener(id, &primary_selection_offer_listener, primary_selection_offer);
  2197. }
  2198. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  2199. ". In zwp_primary_selection_device_v1_listener . primary_selection_device_handle_offer on primary_selection_offer 0x%08x",
  2200. (id ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)id) : -1));
  2201. }
  2202. static void primary_selection_device_handle_selection(void *data, struct zwp_primary_selection_device_v1 *zwp_primary_selection_device_v1,
  2203. struct zwp_primary_selection_offer_v1 *id)
  2204. {
  2205. SDL_WaylandPrimarySelectionDevice *primary_selection_device = data;
  2206. SDL_WaylandPrimarySelectionOffer *offer = NULL;
  2207. if (id) {
  2208. offer = zwp_primary_selection_offer_v1_get_user_data(id);
  2209. }
  2210. if (primary_selection_device->selection_offer != offer) {
  2211. Wayland_primary_selection_offer_destroy(primary_selection_device->selection_offer);
  2212. primary_selection_device->selection_offer = offer;
  2213. }
  2214. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  2215. ". In zwp_primary_selection_device_v1_listener . primary_selection_device_handle_selection on primary_selection_offer 0x%08x",
  2216. (id ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)id) : -1));
  2217. }
  2218. static const struct zwp_primary_selection_device_v1_listener primary_selection_device_listener = {
  2219. primary_selection_device_handle_offer,
  2220. primary_selection_device_handle_selection
  2221. };
  2222. static void text_input_enter(void *data,
  2223. struct zwp_text_input_v3 *zwp_text_input_v3,
  2224. struct wl_surface *surface)
  2225. {
  2226. // No-op
  2227. }
  2228. static void text_input_leave(void *data,
  2229. struct zwp_text_input_v3 *zwp_text_input_v3,
  2230. struct wl_surface *surface)
  2231. {
  2232. // No-op
  2233. }
  2234. static void text_input_preedit_string(void *data,
  2235. struct zwp_text_input_v3 *zwp_text_input_v3,
  2236. const char *text,
  2237. int32_t cursor_begin,
  2238. int32_t cursor_end)
  2239. {
  2240. SDL_WaylandTextInput *text_input = data;
  2241. text_input->has_preedit = true;
  2242. if (text) {
  2243. int cursor_begin_utf8 = cursor_begin >= 0 ? (int)SDL_utf8strnlen(text, cursor_begin) : -1;
  2244. int cursor_end_utf8 = cursor_end >= 0 ? (int)SDL_utf8strnlen(text, cursor_end) : -1;
  2245. int cursor_size_utf8;
  2246. if (cursor_end_utf8 >= 0) {
  2247. if (cursor_begin_utf8 >= 0) {
  2248. cursor_size_utf8 = cursor_end_utf8 - cursor_begin_utf8;
  2249. } else {
  2250. cursor_size_utf8 = cursor_end_utf8;
  2251. }
  2252. } else {
  2253. cursor_size_utf8 = -1;
  2254. }
  2255. SDL_SendEditingText(text, cursor_begin_utf8, cursor_size_utf8);
  2256. } else {
  2257. SDL_SendEditingText("", 0, 0);
  2258. }
  2259. }
  2260. static void text_input_commit_string(void *data,
  2261. struct zwp_text_input_v3 *zwp_text_input_v3,
  2262. const char *text)
  2263. {
  2264. SDL_SendKeyboardText(text);
  2265. }
  2266. static void text_input_delete_surrounding_text(void *data,
  2267. struct zwp_text_input_v3 *zwp_text_input_v3,
  2268. uint32_t before_length,
  2269. uint32_t after_length)
  2270. {
  2271. // FIXME: Do we care about this event?
  2272. }
  2273. static void text_input_done(void *data,
  2274. struct zwp_text_input_v3 *zwp_text_input_v3,
  2275. uint32_t serial)
  2276. {
  2277. SDL_WaylandTextInput *text_input = data;
  2278. if (!text_input->has_preedit) {
  2279. SDL_SendEditingText("", 0, 0);
  2280. }
  2281. text_input->has_preedit = false;
  2282. }
  2283. static const struct zwp_text_input_v3_listener text_input_listener = {
  2284. text_input_enter,
  2285. text_input_leave,
  2286. text_input_preedit_string,
  2287. text_input_commit_string,
  2288. text_input_delete_surrounding_text,
  2289. text_input_done
  2290. };
  2291. void Wayland_create_data_device(SDL_VideoData *d)
  2292. {
  2293. SDL_WaylandDataDevice *data_device = NULL;
  2294. if (!d->input->seat) {
  2295. // No seat yet, will be initialized later.
  2296. return;
  2297. }
  2298. data_device = SDL_calloc(1, sizeof(*data_device));
  2299. if (!data_device) {
  2300. return;
  2301. }
  2302. data_device->data_device = wl_data_device_manager_get_data_device(
  2303. d->data_device_manager, d->input->seat);
  2304. data_device->video_data = d;
  2305. if (!data_device->data_device) {
  2306. SDL_free(data_device);
  2307. } else {
  2308. wl_data_device_set_user_data(data_device->data_device, data_device);
  2309. wl_data_device_add_listener(data_device->data_device,
  2310. &data_device_listener, data_device);
  2311. d->input->data_device = data_device;
  2312. }
  2313. }
  2314. void Wayland_create_primary_selection_device(SDL_VideoData *d)
  2315. {
  2316. SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
  2317. if (!d->input->seat) {
  2318. // No seat yet, will be initialized later.
  2319. return;
  2320. }
  2321. primary_selection_device = SDL_calloc(1, sizeof(*primary_selection_device));
  2322. if (!primary_selection_device) {
  2323. return;
  2324. }
  2325. primary_selection_device->primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(
  2326. d->primary_selection_device_manager, d->input->seat);
  2327. primary_selection_device->video_data = d;
  2328. if (!primary_selection_device->primary_selection_device) {
  2329. SDL_free(primary_selection_device);
  2330. } else {
  2331. zwp_primary_selection_device_v1_set_user_data(primary_selection_device->primary_selection_device,
  2332. primary_selection_device);
  2333. zwp_primary_selection_device_v1_add_listener(primary_selection_device->primary_selection_device,
  2334. &primary_selection_device_listener, primary_selection_device);
  2335. d->input->primary_selection_device = primary_selection_device;
  2336. }
  2337. }
  2338. static void Wayland_create_text_input(SDL_VideoData *d)
  2339. {
  2340. SDL_WaylandTextInput *text_input = NULL;
  2341. if (!d->input->seat) {
  2342. // No seat yet, will be initialized later.
  2343. return;
  2344. }
  2345. text_input = SDL_calloc(1, sizeof(*text_input));
  2346. if (!text_input) {
  2347. return;
  2348. }
  2349. text_input->text_input = zwp_text_input_manager_v3_get_text_input(
  2350. d->text_input_manager, d->input->seat);
  2351. if (!text_input->text_input) {
  2352. SDL_free(text_input);
  2353. } else {
  2354. zwp_text_input_v3_set_user_data(text_input->text_input, text_input);
  2355. zwp_text_input_v3_add_listener(text_input->text_input,
  2356. &text_input_listener, text_input);
  2357. d->input->text_input = text_input;
  2358. }
  2359. }
  2360. void Wayland_create_text_input_manager(SDL_VideoData *d, uint32_t id)
  2361. {
  2362. #ifdef HAVE_FCITX
  2363. const char *im_module = SDL_getenv("SDL_IM_MODULE");
  2364. if (im_module && SDL_strcmp(im_module, "fcitx") == 0) {
  2365. /* Override the Wayland text-input protocol when Fcitx is enabled, like how GTK_IM_MODULE does.
  2366. *
  2367. * The Fcitx wiki discourages enabling it under Wayland via SDL_IM_MODULE, so its presence must
  2368. * be intentional, and this workaround is needed for fixing key repeat detection.
  2369. */
  2370. return;
  2371. }
  2372. #endif
  2373. d->text_input_manager = wl_registry_bind(d->registry, id, &zwp_text_input_manager_v3_interface, 1);
  2374. Wayland_create_text_input(d);
  2375. }
  2376. // Pen/Tablet support...
  2377. typedef struct SDL_WaylandPenTool // a stylus, etc, on a tablet.
  2378. {
  2379. SDL_PenID instance_id;
  2380. SDL_PenInfo info;
  2381. SDL_Window *tool_focus;
  2382. struct zwp_tablet_tool_v2 *wltool;
  2383. float x;
  2384. float y;
  2385. bool frame_motion_set;
  2386. float frame_axes[SDL_PEN_AXIS_COUNT];
  2387. Uint32 frame_axes_set;
  2388. int frame_pen_down;
  2389. int frame_buttons[3];
  2390. } SDL_WaylandPenTool;
  2391. static void tablet_tool_handle_type(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t type)
  2392. {
  2393. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2394. switch (type) {
  2395. #define CASE(typ) case ZWP_TABLET_TOOL_V2_TYPE_##typ: sdltool->info.subtype = SDL_PEN_TYPE_##typ; return
  2396. CASE(ERASER);
  2397. CASE(PEN);
  2398. CASE(PENCIL);
  2399. CASE(AIRBRUSH);
  2400. CASE(BRUSH);
  2401. #undef CASE
  2402. default: sdltool->info.subtype = SDL_PEN_TYPE_UNKNOWN; // we'll decline to add this when the `done` event comes through.
  2403. }
  2404. }
  2405. static void tablet_tool_handle_hardware_serial(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial_hi, uint32_t serial_lo)
  2406. {
  2407. // don't care about this atm.
  2408. }
  2409. static void tablet_tool_handle_hardware_id_wacom(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t id_hi, uint32_t id_lo)
  2410. {
  2411. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2412. sdltool->info.wacom_id = id_lo;
  2413. }
  2414. static void tablet_tool_handle_capability(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t capability)
  2415. {
  2416. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2417. switch (capability) {
  2418. #define CASE(wltyp,sdltyp) case ZWP_TABLET_TOOL_V2_CAPABILITY_##wltyp: sdltool->info.capabilities |= sdltyp; return
  2419. CASE(TILT, SDL_PEN_CAPABILITY_XTILT | SDL_PEN_CAPABILITY_YTILT);
  2420. CASE(PRESSURE, SDL_PEN_CAPABILITY_PRESSURE);
  2421. CASE(DISTANCE, SDL_PEN_CAPABILITY_DISTANCE);
  2422. CASE(ROTATION, SDL_PEN_CAPABILITY_ROTATION);
  2423. CASE(SLIDER, SDL_PEN_CAPABILITY_SLIDER);
  2424. #undef CASE
  2425. default: break; // unsupported here.
  2426. }
  2427. }
  2428. static void tablet_tool_handle_done(void *data, struct zwp_tablet_tool_v2 *tool)
  2429. {
  2430. }
  2431. static void tablet_tool_handle_removed(void *data, struct zwp_tablet_tool_v2 *tool)
  2432. {
  2433. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2434. if (sdltool->instance_id) {
  2435. SDL_RemovePenDevice(0, sdltool->instance_id);
  2436. }
  2437. zwp_tablet_tool_v2_destroy(tool);
  2438. SDL_free(sdltool);
  2439. }
  2440. static void tablet_tool_handle_proximity_in(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial, struct zwp_tablet_v2 *tablet, struct wl_surface *surface)
  2441. {
  2442. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2443. SDL_WindowData *windowdata = surface ? Wayland_GetWindowDataForOwnedSurface(surface) : NULL;
  2444. sdltool->tool_focus = windowdata ? windowdata->sdlwindow : NULL;
  2445. SDL_assert(sdltool->instance_id == 0); // shouldn't be added at this point.
  2446. if (sdltool->info.subtype != SDL_PEN_TYPE_UNKNOWN) { // don't tell SDL about it if we don't know its role.
  2447. sdltool->instance_id = SDL_AddPenDevice(0, NULL, &sdltool->info, sdltool);
  2448. }
  2449. // According to the docs, this should be followed by a motion event, where we'll send our SDL events.
  2450. }
  2451. static void tablet_tool_handle_proximity_out(void *data, struct zwp_tablet_tool_v2 *tool)
  2452. {
  2453. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2454. sdltool->tool_focus = NULL;
  2455. if (sdltool->instance_id) {
  2456. SDL_RemovePenDevice(0, sdltool->instance_id);
  2457. sdltool->instance_id = 0;
  2458. }
  2459. }
  2460. static void tablet_tool_handle_down(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial)
  2461. {
  2462. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2463. sdltool->frame_pen_down = 1;
  2464. }
  2465. static void tablet_tool_handle_up(void *data, struct zwp_tablet_tool_v2 *tool)
  2466. {
  2467. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2468. sdltool->frame_pen_down = 0;
  2469. }
  2470. static void tablet_tool_handle_motion(void *data, struct zwp_tablet_tool_v2 *tool, wl_fixed_t sx_w, wl_fixed_t sy_w)
  2471. {
  2472. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2473. SDL_Window *window = sdltool->tool_focus;
  2474. if (window) {
  2475. const SDL_WindowData *windowdata = window->internal;
  2476. sdltool->x = (float)(wl_fixed_to_double(sx_w) * windowdata->pointer_scale.x);
  2477. sdltool->y = (float)(wl_fixed_to_double(sy_w) * windowdata->pointer_scale.y);
  2478. sdltool->frame_motion_set = true;
  2479. }
  2480. }
  2481. static void tablet_tool_handle_pressure(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t pressure)
  2482. {
  2483. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2484. sdltool->frame_axes[SDL_PEN_AXIS_PRESSURE] = ((float) pressure) / 65535.0f;
  2485. sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_PRESSURE);
  2486. if (pressure) {
  2487. sdltool->frame_axes[SDL_PEN_AXIS_DISTANCE] = 0.0f;
  2488. sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_DISTANCE);
  2489. }
  2490. }
  2491. static void tablet_tool_handle_distance(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t distance)
  2492. {
  2493. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2494. sdltool->frame_axes[SDL_PEN_AXIS_DISTANCE] = ((float) distance) / 65535.0f;
  2495. sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_DISTANCE);
  2496. if (distance) {
  2497. sdltool->frame_axes[SDL_PEN_AXIS_PRESSURE] = 0.0f;
  2498. sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_PRESSURE);
  2499. }
  2500. }
  2501. static void tablet_tool_handle_tilt(void *data, struct zwp_tablet_tool_v2 *tool, wl_fixed_t xtilt, wl_fixed_t ytilt)
  2502. {
  2503. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2504. sdltool->frame_axes[SDL_PEN_AXIS_XTILT] = (float)(wl_fixed_to_double(xtilt));
  2505. sdltool->frame_axes[SDL_PEN_AXIS_YTILT] = (float)(wl_fixed_to_double(ytilt));
  2506. sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_XTILT) | (1u << SDL_PEN_AXIS_YTILT);
  2507. }
  2508. static void tablet_tool_handle_button(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial, uint32_t button, uint32_t state)
  2509. {
  2510. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2511. int sdlbutton;
  2512. switch (button) {
  2513. // see %{_includedir}/linux/input-event-codes.h
  2514. case 0x14b: // BTN_STYLUS
  2515. sdlbutton = 1;
  2516. break;
  2517. case 0x14c: // BTN_STYLUS2
  2518. sdlbutton = 2;
  2519. break;
  2520. case 0x149: // BTN_STYLUS3
  2521. sdlbutton = 3;
  2522. break;
  2523. default:
  2524. return; // don't care about this button, I guess.
  2525. }
  2526. SDL_assert((sdlbutton >= 1) && (sdlbutton <= SDL_arraysize(sdltool->frame_buttons)));
  2527. sdltool->frame_buttons[sdlbutton-1] = (state == ZWP_TABLET_PAD_V2_BUTTON_STATE_PRESSED) ? 1 : 0;
  2528. }
  2529. static void tablet_tool_handle_rotation(void *data, struct zwp_tablet_tool_v2 *tool, wl_fixed_t degrees)
  2530. {
  2531. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2532. const float rotation = (float)(wl_fixed_to_double(degrees));
  2533. sdltool->frame_axes[SDL_PEN_AXIS_ROTATION] = (rotation > 180.0f) ? (rotation - 360.0f) : rotation; // map to -180.0f ... 179.0f range
  2534. sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_ROTATION);
  2535. }
  2536. static void tablet_tool_handle_slider(void *data, struct zwp_tablet_tool_v2 *tool, int32_t position)
  2537. {
  2538. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2539. sdltool->frame_axes[SDL_PEN_AXIS_SLIDER] = position / 65535.f;
  2540. sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_SLIDER);
  2541. }
  2542. static void tablet_tool_handle_wheel(void *data, struct zwp_tablet_tool_v2 *tool, int32_t degrees, int32_t clicks)
  2543. {
  2544. // not supported at the moment
  2545. }
  2546. static void tablet_tool_handle_frame(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t time)
  2547. {
  2548. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
  2549. if (!sdltool->instance_id) {
  2550. return; // Not a pen we report on.
  2551. }
  2552. const Uint64 timestamp = Wayland_GetEventTimestamp(Wayland_EventTimestampMSToNS(time));
  2553. const SDL_PenID instance_id = sdltool->instance_id;
  2554. SDL_Window *window = sdltool->tool_focus;
  2555. // I don't know if this is necessary (or makes sense), but send motion before pen downs, but after pen ups, so you don't get unexpected lines drawn.
  2556. if (sdltool->frame_motion_set && (sdltool->frame_pen_down != -1)) {
  2557. if (sdltool->frame_pen_down) {
  2558. SDL_SendPenMotion(timestamp, instance_id, window, sdltool->x, sdltool->y);
  2559. SDL_SendPenTouch(timestamp, instance_id, window, false, true); // !!! FIXME: how do we know what tip is in use?
  2560. } else {
  2561. SDL_SendPenTouch(timestamp, instance_id, window, false, false); // !!! FIXME: how do we know what tip is in use?
  2562. SDL_SendPenMotion(timestamp, instance_id, window, sdltool->x, sdltool->y);
  2563. }
  2564. } else {
  2565. if (sdltool->frame_pen_down != -1) {
  2566. SDL_SendPenTouch(timestamp, instance_id, window, false, (sdltool->frame_pen_down != 0)); // !!! FIXME: how do we know what tip is in use?
  2567. }
  2568. if (sdltool->frame_motion_set) {
  2569. SDL_SendPenMotion(timestamp, instance_id, window, sdltool->x, sdltool->y);
  2570. }
  2571. }
  2572. for (SDL_PenAxis i = 0; i < SDL_PEN_AXIS_COUNT; i++) {
  2573. if (sdltool->frame_axes_set & (1u << i)) {
  2574. SDL_SendPenAxis(timestamp, instance_id, window, i, sdltool->frame_axes[i]);
  2575. }
  2576. }
  2577. for (int i = 0; i < SDL_arraysize(sdltool->frame_buttons); i++) {
  2578. const int state = sdltool->frame_buttons[i];
  2579. if (state != -1) {
  2580. SDL_SendPenButton(timestamp, instance_id, window, (Uint8)(i + 1), (state != 0));
  2581. sdltool->frame_buttons[i] = -1;
  2582. }
  2583. }
  2584. // reset for next frame.
  2585. sdltool->frame_pen_down = -1;
  2586. sdltool->frame_motion_set = false;
  2587. sdltool->frame_axes_set = 0;
  2588. }
  2589. static const struct zwp_tablet_tool_v2_listener tablet_tool_listener = {
  2590. tablet_tool_handle_type,
  2591. tablet_tool_handle_hardware_serial,
  2592. tablet_tool_handle_hardware_id_wacom,
  2593. tablet_tool_handle_capability,
  2594. tablet_tool_handle_done,
  2595. tablet_tool_handle_removed,
  2596. tablet_tool_handle_proximity_in,
  2597. tablet_tool_handle_proximity_out,
  2598. tablet_tool_handle_down,
  2599. tablet_tool_handle_up,
  2600. tablet_tool_handle_motion,
  2601. tablet_tool_handle_pressure,
  2602. tablet_tool_handle_distance,
  2603. tablet_tool_handle_tilt,
  2604. tablet_tool_handle_rotation,
  2605. tablet_tool_handle_slider,
  2606. tablet_tool_handle_wheel,
  2607. tablet_tool_handle_button,
  2608. tablet_tool_handle_frame
  2609. };
  2610. static void tablet_seat_handle_tablet_added(void *data, struct zwp_tablet_seat_v2 *seat, struct zwp_tablet_v2 *tablet)
  2611. {
  2612. // don't care atm.
  2613. }
  2614. static void tablet_seat_handle_tool_added(void *data, struct zwp_tablet_seat_v2 *seat, struct zwp_tablet_tool_v2 *tool)
  2615. {
  2616. SDL_WaylandPenTool *sdltool = SDL_calloc(1, sizeof(*sdltool));
  2617. if (sdltool) { // if allocation failed, oh well, we won't report this device.
  2618. sdltool->wltool = tool;
  2619. sdltool->info.max_tilt = -1.0f;
  2620. sdltool->info.num_buttons = -1;
  2621. sdltool->frame_pen_down = -1;
  2622. for (int i = 0; i < SDL_arraysize(sdltool->frame_buttons); i++) {
  2623. sdltool->frame_buttons[i] = -1;
  2624. }
  2625. // this will send a bunch of zwp_tablet_tool_v2 events right up front to tell
  2626. // us device details, with a "done" event to let us know we have everything.
  2627. zwp_tablet_tool_v2_add_listener(tool, &tablet_tool_listener, sdltool);
  2628. }
  2629. }
  2630. static void tablet_seat_handle_pad_added(void *data, struct zwp_tablet_seat_v2 *seat, struct zwp_tablet_pad_v2 *pad)
  2631. {
  2632. // we don't care atm.
  2633. }
  2634. static const struct zwp_tablet_seat_v2_listener tablet_seat_listener = {
  2635. tablet_seat_handle_tablet_added,
  2636. tablet_seat_handle_tool_added,
  2637. tablet_seat_handle_pad_added
  2638. };
  2639. void Wayland_input_init_tablet_support(struct SDL_WaylandInput *input, struct zwp_tablet_manager_v2 *tablet_manager)
  2640. {
  2641. if (!tablet_manager || !input->seat) {
  2642. return;
  2643. }
  2644. SDL_WaylandTabletInput *tablet_input = SDL_calloc(1, sizeof(*tablet_input));
  2645. if (!tablet_input) {
  2646. return;
  2647. }
  2648. tablet_input->input = input;
  2649. tablet_input->seat = zwp_tablet_manager_v2_get_tablet_seat(tablet_manager, input->seat);
  2650. zwp_tablet_seat_v2_add_listener(tablet_input->seat, &tablet_seat_listener, tablet_input);
  2651. input->tablet_input = tablet_input;
  2652. }
  2653. static void Wayland_remove_all_pens_callback(SDL_PenID instance_id, void *handle, void *userdata)
  2654. {
  2655. SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) handle;
  2656. zwp_tablet_tool_v2_destroy(sdltool->wltool);
  2657. SDL_free(sdltool);
  2658. }
  2659. void Wayland_input_quit_tablet_support(struct SDL_WaylandInput *input)
  2660. {
  2661. SDL_RemoveAllPenDevices(Wayland_remove_all_pens_callback, NULL);
  2662. if (input && input->tablet_input) {
  2663. zwp_tablet_seat_v2_destroy(input->tablet_input->seat);
  2664. SDL_free(input->tablet_input);
  2665. input->tablet_input = NULL;
  2666. }
  2667. }
  2668. void Wayland_input_initialize_seat(SDL_VideoData *d)
  2669. {
  2670. struct SDL_WaylandInput *input = d->input;
  2671. WAYLAND_wl_list_init(&touch_points);
  2672. if (d->data_device_manager) {
  2673. Wayland_create_data_device(d);
  2674. }
  2675. if (d->primary_selection_device_manager) {
  2676. Wayland_create_primary_selection_device(d);
  2677. }
  2678. if (d->text_input_manager) {
  2679. Wayland_create_text_input(d);
  2680. }
  2681. wl_seat_add_listener(input->seat, &seat_listener, input);
  2682. wl_seat_set_user_data(input->seat, input);
  2683. if (d->tablet_manager) {
  2684. Wayland_input_init_tablet_support(d->input, d->tablet_manager);
  2685. }
  2686. WAYLAND_wl_display_flush(d->display);
  2687. }
  2688. void Wayland_display_destroy_input(SDL_VideoData *d)
  2689. {
  2690. struct SDL_WaylandInput *input = d->input;
  2691. if (input->keyboard_timestamps) {
  2692. zwp_input_timestamps_v1_destroy(input->keyboard_timestamps);
  2693. }
  2694. if (input->pointer_timestamps) {
  2695. zwp_input_timestamps_v1_destroy(input->pointer_timestamps);
  2696. }
  2697. if (input->touch_timestamps) {
  2698. zwp_input_timestamps_v1_destroy(input->touch_timestamps);
  2699. }
  2700. if (input->data_device) {
  2701. Wayland_data_device_clear_selection(input->data_device);
  2702. if (input->data_device->selection_offer) {
  2703. Wayland_data_offer_destroy(input->data_device->selection_offer);
  2704. }
  2705. if (input->data_device->drag_offer) {
  2706. Wayland_data_offer_destroy(input->data_device->drag_offer);
  2707. }
  2708. if (input->data_device->data_device) {
  2709. if (wl_data_device_get_version(input->data_device->data_device) >= WL_DATA_DEVICE_RELEASE_SINCE_VERSION) {
  2710. wl_data_device_release(input->data_device->data_device);
  2711. } else {
  2712. wl_data_device_destroy(input->data_device->data_device);
  2713. }
  2714. }
  2715. SDL_free(input->data_device);
  2716. }
  2717. if (input->primary_selection_device) {
  2718. if (input->primary_selection_device->selection_offer) {
  2719. Wayland_primary_selection_offer_destroy(input->primary_selection_device->selection_offer);
  2720. }
  2721. if (input->primary_selection_device->selection_source) {
  2722. Wayland_primary_selection_source_destroy(input->primary_selection_device->selection_source);
  2723. }
  2724. if (input->primary_selection_device->primary_selection_device) {
  2725. zwp_primary_selection_device_v1_destroy(input->primary_selection_device->primary_selection_device);
  2726. }
  2727. SDL_free(input->primary_selection_device);
  2728. }
  2729. if (input->text_input) {
  2730. zwp_text_input_v3_destroy(input->text_input->text_input);
  2731. SDL_free(input->text_input);
  2732. }
  2733. if (input->keyboard) {
  2734. if (wl_keyboard_get_version(input->keyboard) >= WL_KEYBOARD_RELEASE_SINCE_VERSION) {
  2735. wl_keyboard_release(input->keyboard);
  2736. } else {
  2737. wl_keyboard_destroy(input->keyboard);
  2738. }
  2739. }
  2740. if (input->relative_pointer) {
  2741. zwp_relative_pointer_v1_destroy(input->relative_pointer);
  2742. }
  2743. if (input->cursor_shape) {
  2744. wp_cursor_shape_device_v1_destroy(input->cursor_shape);
  2745. }
  2746. if (input->pointer) {
  2747. if (wl_pointer_get_version(input->pointer) >= WL_POINTER_RELEASE_SINCE_VERSION) {
  2748. wl_pointer_release(input->pointer);
  2749. } else {
  2750. wl_pointer_destroy(input->pointer);
  2751. }
  2752. }
  2753. if (input->touch) {
  2754. struct SDL_WaylandTouchPoint *tp, *tmp;
  2755. SDL_DelTouch(1);
  2756. if (wl_touch_get_version(input->touch) >= WL_TOUCH_RELEASE_SINCE_VERSION) {
  2757. wl_touch_release(input->touch);
  2758. } else {
  2759. wl_touch_destroy(input->touch);
  2760. }
  2761. wl_list_for_each_safe (tp, tmp, &touch_points, link) {
  2762. WAYLAND_wl_list_remove(&tp->link);
  2763. SDL_free(tp);
  2764. }
  2765. }
  2766. if (input->tablet_input) {
  2767. Wayland_input_quit_tablet_support(input);
  2768. }
  2769. if (input->seat) {
  2770. if (wl_seat_get_version(input->seat) >= WL_SEAT_RELEASE_SINCE_VERSION) {
  2771. wl_seat_release(input->seat);
  2772. } else {
  2773. wl_seat_destroy(input->seat);
  2774. }
  2775. }
  2776. if (input->xkb.compose_state) {
  2777. WAYLAND_xkb_compose_state_unref(input->xkb.compose_state);
  2778. }
  2779. if (input->xkb.compose_table) {
  2780. WAYLAND_xkb_compose_table_unref(input->xkb.compose_table);
  2781. }
  2782. if (input->xkb.state) {
  2783. WAYLAND_xkb_state_unref(input->xkb.state);
  2784. }
  2785. if (input->xkb.keymap) {
  2786. WAYLAND_xkb_keymap_unref(input->xkb.keymap);
  2787. }
  2788. SDL_free(input);
  2789. d->input = NULL;
  2790. }
  2791. bool Wayland_input_enable_relative_pointer(struct SDL_WaylandInput *input)
  2792. {
  2793. SDL_VideoDevice *vd = SDL_GetVideoDevice();
  2794. SDL_VideoData *d = input->display;
  2795. SDL_Window *window;
  2796. if (!d->relative_pointer_manager) {
  2797. return false;
  2798. }
  2799. if (!d->pointer_constraints) {
  2800. return false;
  2801. }
  2802. if (!input->pointer) {
  2803. return false;
  2804. }
  2805. /* If we have a pointer confine active, we must destroy it here because
  2806. * creating a locked pointer otherwise would be a protocol error.
  2807. */
  2808. for (window = vd->windows; window; window = window->next) {
  2809. pointer_confine_destroy(window);
  2810. }
  2811. for (window = vd->windows; window; window = window->next) {
  2812. Wayland_input_lock_pointer(input, window);
  2813. }
  2814. d->relative_mouse_mode = 1;
  2815. return true;
  2816. }
  2817. bool Wayland_input_disable_relative_pointer(struct SDL_WaylandInput *input)
  2818. {
  2819. SDL_VideoDevice *vd = SDL_GetVideoDevice();
  2820. SDL_VideoData *d = input->display;
  2821. SDL_Window *window;
  2822. for (window = vd->windows; window; window = window->next) {
  2823. Wayland_input_unlock_pointer(input, window);
  2824. }
  2825. d->relative_mouse_mode = 0;
  2826. for (window = vd->windows; window; window = window->next) {
  2827. Wayland_input_confine_pointer(input, window);
  2828. }
  2829. return true;
  2830. }
  2831. bool Wayland_input_confine_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
  2832. {
  2833. SDL_WindowData *w = window->internal;
  2834. SDL_VideoData *d = input->display;
  2835. struct wl_region *confine_rect;
  2836. if (!d->pointer_constraints) {
  2837. return SDL_SetError("Failed to confine pointer: compositor lacks support for the required zwp_pointer_constraints_v1 protocol");
  2838. }
  2839. if (!input->pointer) {
  2840. return SDL_SetError("No pointer to confine");
  2841. }
  2842. // The confinement region will be created when the window is mapped.
  2843. if (w->shell_surface_status != WAYLAND_SHELL_SURFACE_STATUS_SHOWN) {
  2844. return true;
  2845. }
  2846. /* A confine may already be active, in which case we should destroy it and
  2847. * create a new one.
  2848. */
  2849. pointer_confine_destroy(window);
  2850. /* We cannot create a confine if the pointer is already locked. Defer until
  2851. * the pointer is unlocked.
  2852. */
  2853. if (d->relative_mouse_mode) {
  2854. return true;
  2855. }
  2856. // Don't confine the pointer if it shouldn't be confined.
  2857. if (SDL_RectEmpty(&window->mouse_rect) && !(window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
  2858. return true;
  2859. }
  2860. if (SDL_RectEmpty(&window->mouse_rect)) {
  2861. confine_rect = NULL;
  2862. } else {
  2863. SDL_Rect scaled_mouse_rect;
  2864. scaled_mouse_rect.x = (int)SDL_floor(window->mouse_rect.x / w->pointer_scale.x);
  2865. scaled_mouse_rect.y = (int)SDL_floor(window->mouse_rect.y / w->pointer_scale.y);
  2866. scaled_mouse_rect.w = (int)SDL_ceil(window->mouse_rect.w / w->pointer_scale.x);
  2867. scaled_mouse_rect.h = (int)SDL_ceil(window->mouse_rect.h / w->pointer_scale.y);
  2868. confine_rect = wl_compositor_create_region(d->compositor);
  2869. wl_region_add(confine_rect,
  2870. scaled_mouse_rect.x,
  2871. scaled_mouse_rect.y,
  2872. scaled_mouse_rect.w,
  2873. scaled_mouse_rect.h);
  2874. }
  2875. struct zwp_confined_pointer_v1 *confined_pointer =
  2876. zwp_pointer_constraints_v1_confine_pointer(d->pointer_constraints,
  2877. w->surface,
  2878. input->pointer,
  2879. confine_rect,
  2880. ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
  2881. zwp_confined_pointer_v1_add_listener(confined_pointer,
  2882. &confined_pointer_listener,
  2883. window);
  2884. if (confine_rect) {
  2885. wl_region_destroy(confine_rect);
  2886. }
  2887. // Commit the double-buffered confinement region.
  2888. wl_surface_commit(w->surface);
  2889. w->confined_pointer = confined_pointer;
  2890. return true;
  2891. }
  2892. bool Wayland_input_unconfine_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
  2893. {
  2894. pointer_confine_destroy(window);
  2895. return true;
  2896. }
  2897. bool Wayland_input_grab_keyboard(SDL_Window *window, struct SDL_WaylandInput *input)
  2898. {
  2899. SDL_WindowData *w = window->internal;
  2900. SDL_VideoData *d = input->display;
  2901. if (!d->key_inhibitor_manager) {
  2902. return SDL_SetError("Failed to grab keyboard: compositor lacks support for the required zwp_keyboard_shortcuts_inhibit_manager_v1 protocol");
  2903. }
  2904. if (w->key_inhibitor) {
  2905. return true;
  2906. }
  2907. w->key_inhibitor =
  2908. zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(d->key_inhibitor_manager,
  2909. w->surface,
  2910. input->seat);
  2911. return true;
  2912. }
  2913. bool Wayland_input_ungrab_keyboard(SDL_Window *window)
  2914. {
  2915. SDL_WindowData *w = window->internal;
  2916. if (w->key_inhibitor) {
  2917. zwp_keyboard_shortcuts_inhibitor_v1_destroy(w->key_inhibitor);
  2918. w->key_inhibitor = NULL;
  2919. }
  2920. return true;
  2921. }
  2922. void Wayland_UpdateImplicitGrabSerial(struct SDL_WaylandInput *input, Uint32 serial)
  2923. {
  2924. if (serial > input->last_implicit_grab_serial) {
  2925. input->last_implicit_grab_serial = serial;
  2926. Wayland_data_device_set_serial(input->data_device, serial);
  2927. Wayland_primary_selection_device_set_serial(input->primary_selection_device, serial);
  2928. }
  2929. }
  2930. #endif // SDL_VIDEO_DRIVER_WAYLAND