2
0

shim.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902
  1. /*
  2. * Copyright (c) Milan Jurkulák 2026.
  3. * Contact:
  4. * e: mimoccc@gmail.com
  5. * e: mj@mjdev.org
  6. * w: https://mjdev.org
  7. *
  8. * wlroots 0.18 based compositor core, exposed to Kotlin/Native
  9. * through the flat API in shim.h. Policy (layers, focus order,
  10. * IPC) lives on the Kotlin side.
  11. */
  12. #define _POSIX_C_SOURCE 200809L
  13. #include <assert.h>
  14. #include <signal.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/wait.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <wayland-server-core.h>
  24. #include <wlr/backend.h>
  25. #include <wlr/backend/session.h>
  26. #include <wlr/render/allocator.h>
  27. #include <wlr/render/wlr_renderer.h>
  28. #include <wlr/render/gles2.h>
  29. #include <wlr/render/egl.h>
  30. #include <EGL/egl.h>
  31. #include <GLES2/gl2.h>
  32. #include <wlr/types/wlr_compositor.h>
  33. #include <wlr/types/wlr_subcompositor.h>
  34. #include <wlr/types/wlr_cursor.h>
  35. #include <wlr/types/wlr_data_device.h>
  36. #include <wlr/types/wlr_input_device.h>
  37. #include <wlr/types/wlr_keyboard.h>
  38. #include <wlr/types/wlr_output.h>
  39. #include <wlr/types/wlr_output_layout.h>
  40. #include <wlr/types/wlr_pointer.h>
  41. #include <wlr/types/wlr_scene.h>
  42. #include <wlr/types/wlr_seat.h>
  43. #include <wlr/types/wlr_xcursor_manager.h>
  44. #include <wlr/types/wlr_xdg_decoration_v1.h>
  45. #include <wlr/types/wlr_xdg_shell.h>
  46. #include <wlr/util/log.h>
  47. #include <wlr/xwayland.h>
  48. #include <xkbcommon/xkbcommon.h>
  49. /* server-side window decorations: rendered with cairo into an image surface that
  50. * is wrapped in a custom wlr_buffer and shown via a wlr_scene_buffer node */
  51. #include <math.h>
  52. #include <cairo.h>
  53. #include <drm_fourcc.h>
  54. #include <wlr/interfaces/wlr_buffer.h>
  55. /* _POSIX_C_SOURCE hides the XOPEN/GNU math constants from <math.h> */
  56. #ifndef M_PI
  57. #define M_PI 3.14159265358979323846
  58. #endif
  59. #include "shim.h"
  60. /* GNOME-style decoration geometry (logical px) */
  61. #define MJC_DECO_TITLEBAR_H 36
  62. #define MJC_DECO_RADIUS 10
  63. #define MJC_DECO_BORDER 3
  64. #define MJC_DECO_BTN_R 9 /* button circle radius */
  65. #define MJC_DECO_BTN_GAP 8 /* gap between button circles */
  66. #define MJC_DECO_PAD 14 /* horizontal padding (title left, buttons right) */
  67. #define MJC_DECO_RESIZE 6 /* edge/corner resize hotzone half-width (px) */
  68. /* glass titlebar: matches DesktopPanel verticalGradient (0.2 / 0.4 / 0.5) + BlurPanel sheen */
  69. #define MJC_DECO_GLASS_A0 0.20f
  70. #define MJC_DECO_GLASS_A1 0.40f
  71. #define MJC_DECO_GLASS_A2 0.50f
  72. #define MJC_DECO_GLASS_SHEEN0 0.40f
  73. #define MJC_DECO_GLASS_SHEEN1 0.10f
  74. #define MJC_DECO_BORDER_A 0.55f /* frame border alpha (dock shadow uses ~0.3) */
  75. enum mjc_cursor_mode {
  76. MJC_CURSOR_PASSTHROUGH,
  77. MJC_CURSOR_MOVE,
  78. MJC_CURSOR_RESIZE,
  79. };
  80. struct mjc_server {
  81. struct wl_display *display;
  82. struct wl_event_loop *loop;
  83. struct wlr_backend *backend;
  84. struct wlr_session *session;
  85. struct wlr_renderer *renderer;
  86. struct wlr_allocator *allocator;
  87. struct wlr_compositor *compositor;
  88. struct wlr_scene *scene;
  89. struct wlr_scene_output_layout *scene_layout;
  90. struct wlr_output_layout *output_layout;
  91. struct wlr_scene_tree *layers[5];
  92. struct wlr_xdg_shell *xdg_shell;
  93. struct wlr_xdg_decoration_manager_v1 *xdg_decoration_mgr;
  94. struct wlr_xwayland *xwayland;
  95. struct wlr_cursor *cursor;
  96. struct wlr_xcursor_manager *cursor_mgr;
  97. struct wlr_seat *seat;
  98. struct wl_list views; /* mjc_view.link */
  99. struct wl_list keyboards; /* mjc_keyboard.link */
  100. struct wl_list fd_sources;/* mjc_fd_source.link */
  101. struct mjc_view *focused_view;
  102. mjc_callbacks cbs;
  103. void *ud;
  104. const char *socket;
  105. uint64_t next_view_id;
  106. /* nested-output size (16:9 default); used when the backend has no mode list
  107. * (wayland/headless) and to lock the window against host-driven resizes */
  108. int output_w, output_h;
  109. /* throttle timestamp (msec) for the pointer callback */
  110. uint32_t last_pointer_ms;
  111. /* injection feedback marker (Android "show touches" style): a small white square in
  112. * the overlay layer that appears at the injected pointer and fades out after a timeout */
  113. struct wlr_scene_rect *inject_marker;
  114. struct wl_event_source *inject_fade_timer;
  115. float inject_alpha;
  116. enum mjc_cursor_mode cursor_mode;
  117. struct mjc_view *grabbed_view;
  118. double grab_x, grab_y;
  119. struct wlr_box grab_geobox;
  120. uint32_t resize_edges;
  121. struct wl_event_source *sigchld_source;
  122. /* pids spawned via mjc_spawn; sigchld must reap only these,
  123. * other children (Xwayland) belong to wlroots */
  124. pid_t spawned_pids[64];
  125. int spawned_count;
  126. /* server-side decoration theme (straight-alpha colors, 0..1) derived from the
  127. * shell background; the compositor paints every normal-layer window's frame with it */
  128. float deco_bg[3];
  129. float deco_fg[3];
  130. /* icon circle + frame border colors (shell palette iconsTintColor / borderColor) */
  131. float deco_icon_bg[3];
  132. float deco_icon_fg[3];
  133. struct wl_listener new_output;
  134. struct wl_listener new_xdg_toplevel;
  135. struct wl_listener new_xdg_popup;
  136. struct wl_listener new_toplevel_decoration;
  137. struct wl_listener new_xwayland_surface;
  138. struct wl_listener xwayland_ready;
  139. struct wl_listener cursor_motion;
  140. struct wl_listener cursor_motion_absolute;
  141. struct wl_listener cursor_button;
  142. struct wl_listener cursor_axis;
  143. struct wl_listener cursor_frame;
  144. struct wl_listener new_input;
  145. struct wl_listener request_cursor;
  146. struct wl_listener request_set_selection;
  147. };
  148. struct mjc_view {
  149. struct wl_list link;
  150. struct mjc_server *server;
  151. uint64_t id;
  152. bool is_xwayland;
  153. bool mapped;
  154. bool minimized;
  155. bool maximized;
  156. /* re-center once the client commits its post-unmaximize size */
  157. bool pending_center;
  158. bool placement_set; /* explicit geometry (restore/ipc) — skip auto-center */
  159. bool focusable;
  160. mjc_layer layer;
  161. struct wlr_scene_tree *scene_tree;
  162. /* floating geometry remembered while maximized (x/y are scene coords) */
  163. struct wlr_box saved_geo;
  164. /* server-side decoration (only for normal-layer app windows). The titlebar is a
  165. * cairo buffer node, the borders are scene rects; all are children of scene_tree so
  166. * they move/raise/minimize with the window automatically. */
  167. bool decorated;
  168. bool deco_disabled; /* chromeless: never draw a server frame */
  169. bool is_chrome; /* shell surface (wallpaper/panel/menu): no frame */
  170. struct wlr_scene_buffer *deco_titlebar;
  171. struct wlr_scene_rect *deco_border[3]; /* left, right, bottom */
  172. int deco_w, deco_h; /* last rendered content size */
  173. int hovered_button; /* -1 none, 0 min, 1 max, 2 close */
  174. /* xdg shell */
  175. struct wlr_xdg_toplevel *xdg_toplevel;
  176. /* xwayland */
  177. struct wlr_xwayland_surface *xsurface;
  178. struct wl_listener map;
  179. struct wl_listener unmap;
  180. struct wl_listener commit;
  181. struct wl_listener destroy;
  182. struct wl_listener request_move;
  183. struct wl_listener request_resize;
  184. struct wl_listener request_maximize;
  185. struct wl_listener request_minimize;
  186. struct wl_listener request_fullscreen;
  187. struct wl_listener set_title;
  188. struct wl_listener set_app_id; /* set_class for xwayland */
  189. struct wl_listener associate;
  190. struct wl_listener dissociate;
  191. struct wl_listener request_configure;
  192. struct wl_listener set_geometry;
  193. };
  194. struct mjc_popup {
  195. struct wlr_xdg_popup *xdg_popup;
  196. struct wl_listener commit;
  197. struct wl_listener destroy;
  198. };
  199. struct mjc_decoration {
  200. struct wlr_xdg_toplevel_decoration_v1 *decoration;
  201. struct mjc_view *view;
  202. struct wl_listener request_mode;
  203. struct wl_listener destroy;
  204. };
  205. struct mjc_keyboard {
  206. struct wl_list link;
  207. struct mjc_server *server;
  208. struct wlr_keyboard *wlr_keyboard;
  209. struct wl_listener modifiers;
  210. struct wl_listener key;
  211. struct wl_listener destroy;
  212. };
  213. struct mjc_output {
  214. struct wl_list link;
  215. struct mjc_server *server;
  216. struct wlr_output *wlr_output;
  217. struct wl_listener frame;
  218. struct wl_listener request_state;
  219. struct wl_listener destroy;
  220. };
  221. struct mjc_fd_source {
  222. struct wl_list link;
  223. struct mjc_server *server;
  224. int fd;
  225. struct wl_event_source *source;
  226. };
  227. /* ------------------------------------------------------------------ */
  228. /* helpers */
  229. /* ------------------------------------------------------------------ */
  230. static struct wlr_surface *view_surface(struct mjc_view *view) {
  231. if (view->is_xwayland) {
  232. return view->xsurface != NULL ? view->xsurface->surface : NULL;
  233. }
  234. return view->xdg_toplevel->base->surface;
  235. }
  236. /* Finds the scene buffer node that displays the view's main client surface. */
  237. static struct wlr_scene_buffer *find_buffer_for_surface(struct wlr_scene_node *node,
  238. struct wlr_surface *target) {
  239. if (node->type == WLR_SCENE_NODE_BUFFER) {
  240. struct wlr_scene_buffer *buf = wlr_scene_buffer_from_node(node);
  241. struct wlr_scene_surface *ss = wlr_scene_surface_try_from_buffer(buf);
  242. if (ss != NULL && ss->surface == target) {
  243. return buf;
  244. }
  245. } else if (node->type == WLR_SCENE_NODE_TREE) {
  246. struct wlr_scene_tree *tree = wlr_scene_tree_from_node(node);
  247. struct wlr_scene_node *child;
  248. wl_list_for_each(child, &tree->children, link) {
  249. struct wlr_scene_buffer *found = find_buffer_for_surface(child, target);
  250. if (found != NULL) {
  251. return found;
  252. }
  253. }
  254. }
  255. return NULL;
  256. }
  257. /* Client surface position/size inside the view's scene_tree (not layout space). */
  258. static bool view_client_surface_box(struct mjc_view *view, struct wlr_box *local) {
  259. struct wlr_surface *target = view_surface(view);
  260. if (target == NULL || view->scene_tree == NULL) {
  261. return false;
  262. }
  263. struct wlr_scene_buffer *buf =
  264. find_buffer_for_surface(&view->scene_tree->node, target);
  265. if (buf == NULL) {
  266. return false;
  267. }
  268. int tree_x, tree_y, buf_x, buf_y;
  269. wlr_scene_node_coords(&view->scene_tree->node, &tree_x, &tree_y);
  270. wlr_scene_node_coords(&buf->node, &buf_x, &buf_y);
  271. local->x = buf_x - tree_x;
  272. local->y = buf_y - tree_y;
  273. if (buf->dst_width > 0 && buf->dst_height > 0) {
  274. local->width = buf->dst_width;
  275. local->height = buf->dst_height;
  276. } else if (buf->buffer != NULL) {
  277. local->width = (int) buf->buffer->width;
  278. local->height = (int) buf->buffer->height;
  279. } else if (target->current.width > 0 && target->current.height > 0) {
  280. local->width = target->current.width;
  281. local->height = target->current.height;
  282. } else {
  283. return false;
  284. }
  285. return true;
  286. }
  287. /* re-render a window's titlebar bitmap in place (focus/hover/title changes) */
  288. static void deco_repaint(struct mjc_view *view);
  289. static void emit_focus_change(struct mjc_server *server) {
  290. if (server->cbs.focus_change != NULL) {
  291. server->cbs.focus_change(server->ud, server->focused_view);
  292. }
  293. }
  294. static void view_deactivate(struct mjc_view *view) {
  295. if (view->is_xwayland) {
  296. if (view->xsurface != NULL) {
  297. wlr_xwayland_surface_activate(view->xsurface, false);
  298. }
  299. } else {
  300. wlr_xdg_toplevel_set_activated(view->xdg_toplevel, false);
  301. }
  302. }
  303. static void view_focus_internal(struct mjc_view *view, bool raise) {
  304. if (view == NULL || !view->focusable || view->minimized) {
  305. return;
  306. }
  307. struct mjc_server *server = view->server;
  308. struct wlr_seat *seat = server->seat;
  309. if (server->focused_view == view) {
  310. return;
  311. }
  312. struct mjc_view *prev = server->focused_view;
  313. if (server->focused_view != NULL) {
  314. view_deactivate(server->focused_view);
  315. }
  316. /* focus-follows-mouse must not restack windows (no autoraise); only an explicit
  317. * focus (click / activate / unminimize) raises the view to the top */
  318. if (raise && view->scene_tree != NULL) {
  319. wlr_scene_node_raise_to_top(&view->scene_tree->node);
  320. }
  321. if (view->is_xwayland) {
  322. wlr_xwayland_surface_activate(view->xsurface, true);
  323. wlr_xwayland_surface_restack(view->xsurface, NULL, XCB_STACK_MODE_ABOVE);
  324. } else {
  325. wlr_xdg_toplevel_set_activated(view->xdg_toplevel, true);
  326. }
  327. struct wlr_surface *surface = view_surface(view);
  328. struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat);
  329. if (surface != NULL) {
  330. if (keyboard != NULL) {
  331. wlr_seat_keyboard_notify_enter(seat, surface,
  332. keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers);
  333. } else {
  334. wlr_seat_keyboard_notify_enter(seat, surface, NULL, 0, NULL);
  335. }
  336. }
  337. server->focused_view = view;
  338. /* refresh active/inactive titlebar styling on both windows */
  339. if (prev != NULL) {
  340. deco_repaint(prev);
  341. }
  342. deco_repaint(view);
  343. emit_focus_change(server);
  344. }
  345. void mjc_view_focus(struct mjc_view *view) {
  346. view_focus_internal(view, true);
  347. }
  348. static void clear_focus_if(struct mjc_server *server, struct mjc_view *view) {
  349. if (server->focused_view == view) {
  350. server->focused_view = NULL;
  351. emit_focus_change(server);
  352. }
  353. if (server->grabbed_view == view) {
  354. server->grabbed_view = NULL;
  355. server->cursor_mode = MJC_CURSOR_PASSTHROUGH;
  356. }
  357. }
  358. static struct mjc_view *desktop_view_at(struct mjc_server *server,
  359. double lx, double ly, struct wlr_surface **surface,
  360. double *sx, double *sy) {
  361. struct wlr_scene_node *node =
  362. wlr_scene_node_at(&server->scene->tree.node, lx, ly, sx, sy);
  363. if (node == NULL || node->type != WLR_SCENE_NODE_BUFFER) {
  364. return NULL;
  365. }
  366. struct wlr_scene_buffer *scene_buffer = wlr_scene_buffer_from_node(node);
  367. struct wlr_scene_surface *scene_surface =
  368. wlr_scene_surface_try_from_buffer(scene_buffer);
  369. if (scene_surface == NULL) {
  370. return NULL;
  371. }
  372. *surface = scene_surface->surface;
  373. struct wlr_scene_tree *tree = node->parent;
  374. while (tree != NULL && tree->node.data == NULL) {
  375. tree = tree->node.parent;
  376. }
  377. return tree != NULL ? tree->node.data : NULL;
  378. }
  379. /* ------------------------------------------------------------------ */
  380. /* outputs */
  381. /* ------------------------------------------------------------------ */
  382. static void output_frame(struct wl_listener *listener, void *data) {
  383. (void) data;
  384. struct mjc_output *output = wl_container_of(listener, output, frame);
  385. struct wlr_scene_output *scene_output = wlr_scene_get_scene_output(
  386. output->server->scene, output->wlr_output);
  387. if (scene_output == NULL) {
  388. return;
  389. }
  390. wlr_scene_output_commit(scene_output, NULL);
  391. struct timespec now;
  392. clock_gettime(CLOCK_MONOTONIC, &now);
  393. wlr_scene_output_send_frame_done(scene_output, &now);
  394. }
  395. static void output_request_state(struct wl_listener *listener, void *data) {
  396. struct mjc_output *output = wl_container_of(listener, output, request_state);
  397. const struct wlr_output_event_request_state *event = data;
  398. /* honor host-driven resizes (the user resizing the nested x11/wayland window) so the
  399. * desktop relayouts to the new size; commit the requested state and remember the new
  400. * dimensions, which the shell reads back via the wl_output geometry */
  401. wlr_output_commit_state(output->wlr_output, event->state);
  402. output->server->output_w = output->wlr_output->width;
  403. output->server->output_h = output->wlr_output->height;
  404. }
  405. static void output_destroy(struct wl_listener *listener, void *data) {
  406. (void) data;
  407. struct mjc_output *output = wl_container_of(listener, output, destroy);
  408. struct mjc_server *server = output->server;
  409. wl_list_remove(&output->frame.link);
  410. wl_list_remove(&output->request_state.link);
  411. wl_list_remove(&output->destroy.link);
  412. wl_list_remove(&output->link);
  413. free(output);
  414. /* nested backends (x11/wayland) have no session; closing the host window
  415. * destroys the output, so quit the compositor and let the run action finish.
  416. * a real (drm) session keeps running when an output is unplugged. */
  417. if (server->session == NULL) {
  418. wl_display_terminate(server->display);
  419. }
  420. }
  421. static void server_new_output(struct wl_listener *listener, void *data) {
  422. struct mjc_server *server = wl_container_of(listener, server, new_output);
  423. struct wlr_output *wlr_output = data;
  424. wlr_output_init_render(wlr_output, server->allocator, server->renderer);
  425. struct wlr_output_state state;
  426. wlr_output_state_init(&state);
  427. wlr_output_state_set_enabled(&state, true);
  428. struct wlr_output_mode *mode = wlr_output_preferred_mode(wlr_output);
  429. if (mode != NULL) {
  430. wlr_output_state_set_mode(&state, mode);
  431. } else {
  432. /* wayland / headless backends expose no mode list, so preferred_mode is
  433. * NULL; without a mode the output has no dimensions and renders black.
  434. * pin it to the configured 16:9 size. */
  435. wlr_output_state_set_custom_mode(&state,
  436. server->output_w, server->output_h, 0);
  437. }
  438. wlr_output_commit_state(wlr_output, &state);
  439. wlr_output_state_finish(&state);
  440. struct mjc_output *output = calloc(1, sizeof(*output));
  441. output->wlr_output = wlr_output;
  442. output->server = server;
  443. output->frame.notify = output_frame;
  444. wl_signal_add(&wlr_output->events.frame, &output->frame);
  445. output->request_state.notify = output_request_state;
  446. wl_signal_add(&wlr_output->events.request_state, &output->request_state);
  447. output->destroy.notify = output_destroy;
  448. wl_signal_add(&wlr_output->events.destroy, &output->destroy);
  449. wl_list_init(&output->link);
  450. struct wlr_output_layout_output *l_output =
  451. wlr_output_layout_add_auto(server->output_layout, wlr_output);
  452. struct wlr_scene_output *scene_output =
  453. wlr_scene_output_create(server->scene, wlr_output);
  454. wlr_scene_output_layout_add_output(server->scene_layout, l_output, scene_output);
  455. }
  456. /* ------------------------------------------------------------------ */
  457. /* keyboard & input */
  458. /* ------------------------------------------------------------------ */
  459. static void keyboard_handle_modifiers(struct wl_listener *listener, void *data) {
  460. (void) data;
  461. struct mjc_keyboard *keyboard = wl_container_of(listener, keyboard, modifiers);
  462. wlr_seat_set_keyboard(keyboard->server->seat, keyboard->wlr_keyboard);
  463. wlr_seat_keyboard_notify_modifiers(keyboard->server->seat,
  464. &keyboard->wlr_keyboard->modifiers);
  465. }
  466. static void keyboard_handle_key(struct wl_listener *listener, void *data) {
  467. struct mjc_keyboard *keyboard = wl_container_of(listener, keyboard, key);
  468. struct mjc_server *server = keyboard->server;
  469. struct wlr_keyboard_key_event *event = data;
  470. uint32_t keycode = event->keycode + 8;
  471. const xkb_keysym_t *syms;
  472. int nsyms = xkb_state_key_get_syms(
  473. keyboard->wlr_keyboard->xkb_state, keycode, &syms);
  474. uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->wlr_keyboard);
  475. bool pressed = event->state == WL_KEYBOARD_KEY_STATE_PRESSED;
  476. bool handled = false;
  477. for (int i = 0; i < nsyms; i++) {
  478. /* VT switching always works */
  479. if (pressed && syms[i] >= XKB_KEY_XF86Switch_VT_1 &&
  480. syms[i] <= XKB_KEY_XF86Switch_VT_12) {
  481. if (server->session != NULL) {
  482. wlr_session_change_vt(server->session,
  483. syms[i] - XKB_KEY_XF86Switch_VT_1 + 1);
  484. }
  485. handled = true;
  486. continue;
  487. }
  488. if (server->cbs.key != NULL &&
  489. server->cbs.key(server->ud, syms[i], modifiers, pressed)) {
  490. handled = true;
  491. }
  492. }
  493. if (!handled) {
  494. wlr_seat_set_keyboard(server->seat, keyboard->wlr_keyboard);
  495. wlr_seat_keyboard_notify_key(server->seat,
  496. event->time_msec, event->keycode, event->state);
  497. }
  498. }
  499. static void keyboard_handle_destroy(struct wl_listener *listener, void *data) {
  500. (void) data;
  501. struct mjc_keyboard *keyboard = wl_container_of(listener, keyboard, destroy);
  502. wl_list_remove(&keyboard->modifiers.link);
  503. wl_list_remove(&keyboard->key.link);
  504. wl_list_remove(&keyboard->destroy.link);
  505. wl_list_remove(&keyboard->link);
  506. free(keyboard);
  507. }
  508. static void server_new_keyboard(struct mjc_server *server,
  509. struct wlr_input_device *device) {
  510. struct wlr_keyboard *wlr_keyboard = wlr_keyboard_from_input_device(device);
  511. struct mjc_keyboard *keyboard = calloc(1, sizeof(*keyboard));
  512. keyboard->server = server;
  513. keyboard->wlr_keyboard = wlr_keyboard;
  514. struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
  515. struct xkb_keymap *keymap = xkb_keymap_new_from_names(context, NULL,
  516. XKB_KEYMAP_COMPILE_NO_FLAGS);
  517. wlr_keyboard_set_keymap(wlr_keyboard, keymap);
  518. xkb_keymap_unref(keymap);
  519. xkb_context_unref(context);
  520. wlr_keyboard_set_repeat_info(wlr_keyboard, 25, 600);
  521. keyboard->modifiers.notify = keyboard_handle_modifiers;
  522. wl_signal_add(&wlr_keyboard->events.modifiers, &keyboard->modifiers);
  523. keyboard->key.notify = keyboard_handle_key;
  524. wl_signal_add(&wlr_keyboard->events.key, &keyboard->key);
  525. keyboard->destroy.notify = keyboard_handle_destroy;
  526. wl_signal_add(&device->events.destroy, &keyboard->destroy);
  527. wlr_seat_set_keyboard(server->seat, wlr_keyboard);
  528. wl_list_insert(&server->keyboards, &keyboard->link);
  529. }
  530. static void server_new_pointer(struct mjc_server *server,
  531. struct wlr_input_device *device) {
  532. wlr_cursor_attach_input_device(server->cursor, device);
  533. }
  534. static void server_new_input(struct wl_listener *listener, void *data) {
  535. struct mjc_server *server = wl_container_of(listener, server, new_input);
  536. struct wlr_input_device *device = data;
  537. switch (device->type) {
  538. case WLR_INPUT_DEVICE_KEYBOARD:
  539. server_new_keyboard(server, device);
  540. break;
  541. case WLR_INPUT_DEVICE_POINTER:
  542. server_new_pointer(server, device);
  543. break;
  544. default:
  545. break;
  546. }
  547. uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
  548. if (!wl_list_empty(&server->keyboards)) {
  549. caps |= WL_SEAT_CAPABILITY_KEYBOARD;
  550. }
  551. wlr_seat_set_capabilities(server->seat, caps);
  552. }
  553. static void seat_request_cursor(struct wl_listener *listener, void *data) {
  554. struct mjc_server *server = wl_container_of(listener, server, request_cursor);
  555. struct wlr_seat_pointer_request_set_cursor_event *event = data;
  556. struct wlr_seat_client *focused_client =
  557. server->seat->pointer_state.focused_client;
  558. if (focused_client == event->seat_client) {
  559. wlr_cursor_set_surface(server->cursor, event->surface,
  560. event->hotspot_x, event->hotspot_y);
  561. }
  562. }
  563. static void seat_request_set_selection(struct wl_listener *listener, void *data) {
  564. struct mjc_server *server =
  565. wl_container_of(listener, server, request_set_selection);
  566. struct wlr_seat_request_set_selection_event *event = data;
  567. wlr_seat_set_selection(server->seat, event->source, event->serial);
  568. }
  569. /* ------------------------------------------------------------------ */
  570. /* cursor */
  571. /* ------------------------------------------------------------------ */
  572. static void view_current_geometry(struct mjc_view *view, struct wlr_box *box);
  573. static bool view_should_decorate(struct mjc_view *view);
  574. static void view_deco_insets(struct mjc_view *view,
  575. int *top, int *left, int *right, int *bottom);
  576. static void view_visible_geometry(struct mjc_view *view, struct wlr_box *box);
  577. static void view_place_visible_topleft(struct mjc_view *view, int vx, int vy);
  578. static bool view_deco_content_metrics(struct mjc_view *view, int *cw, int *ch);
  579. static void view_deco_snap_client(struct mjc_view *view);
  580. static void view_geo_offset(struct mjc_view *view, int *ox, int *oy);
  581. static bool view_deco_frame_box(struct mjc_view *view, struct wlr_box *frame);
  582. /* Keep the outer frame as visible as possible on the primary output. */
  583. static void view_clamp_frame_pos(struct mjc_server *server,
  584. int *x, int *y, int width, int height) {
  585. struct wlr_box out;
  586. wlr_output_layout_get_box(server->output_layout, NULL, &out);
  587. if (out.width <= 0 || out.height <= 0 || width <= 0 || height <= 0) {
  588. return;
  589. }
  590. if (*x < out.x) {
  591. *x = out.x;
  592. }
  593. if (*y < out.y) {
  594. *y = out.y;
  595. }
  596. if (*x + width > out.x + out.width) {
  597. *x = out.x + out.width - width;
  598. }
  599. if (*y + height > out.y + out.height) {
  600. *y = out.y + out.height - height;
  601. }
  602. if (*x < out.x) {
  603. *x = out.x;
  604. }
  605. if (*y < out.y) {
  606. *y = out.y;
  607. }
  608. }
  609. /* server-side decoration helpers (defined after the interactive-grab section) */
  610. static void view_decoration_update(struct mjc_view *view);
  611. static void view_decoration_destroy(struct mjc_view *view);
  612. /* returns true when the press/release was consumed by a window's decoration */
  613. static bool deco_handle_button(struct mjc_server *server,
  614. double lx, double ly, bool pressed);
  615. /* recompute which titlebar button (if any) the pointer hovers, re-rendering on change */
  616. static void deco_update_hover(struct mjc_server *server);
  617. /* layout-space top-left and width of a decorated view's titlebar; false if undecorated */
  618. static bool deco_titlebar_box(struct mjc_view *view,
  619. double *tx, double *ty, int *w);
  620. /* resize-edge mask under (lx,ly) for a decorated window (0 = not on an edge) */
  621. static uint32_t deco_resize_edges(struct mjc_server *server,
  622. double lx, double ly, struct mjc_view **out);
  623. /* xcursor name for a resize-edge mask */
  624. static const char *deco_resize_cursor(uint32_t edges);
  625. static void process_cursor_move(struct mjc_server *server) {
  626. struct mjc_view *view = server->grabbed_view;
  627. if (view == NULL || view->scene_tree == NULL) {
  628. return;
  629. }
  630. int x = (int)(server->cursor->x - server->grab_x);
  631. int y = (int)(server->cursor->y - server->grab_y);
  632. wlr_scene_node_set_position(&view->scene_tree->node, x, y);
  633. if (view->is_xwayland && view->xsurface != NULL) {
  634. wlr_xwayland_surface_configure(view->xsurface,
  635. (int16_t) x, (int16_t) y,
  636. (uint16_t) view->xsurface->width, (uint16_t) view->xsurface->height);
  637. }
  638. }
  639. static void process_cursor_resize(struct mjc_server *server) {
  640. struct mjc_view *view = server->grabbed_view;
  641. if (view == NULL || view->scene_tree == NULL) {
  642. return;
  643. }
  644. double border_x = server->cursor->x - server->grab_x;
  645. double border_y = server->cursor->y - server->grab_y;
  646. int new_left = server->grab_geobox.x;
  647. int new_right = server->grab_geobox.x + server->grab_geobox.width;
  648. int new_top = server->grab_geobox.y;
  649. int new_bottom = server->grab_geobox.y + server->grab_geobox.height;
  650. if (server->resize_edges & WLR_EDGE_TOP) {
  651. new_top = (int) border_y;
  652. if (new_top >= new_bottom) {
  653. new_top = new_bottom - 1;
  654. }
  655. } else if (server->resize_edges & WLR_EDGE_BOTTOM) {
  656. new_bottom = (int) border_y;
  657. if (new_bottom <= new_top) {
  658. new_bottom = new_top + 1;
  659. }
  660. }
  661. if (server->resize_edges & WLR_EDGE_LEFT) {
  662. new_left = (int) border_x;
  663. if (new_left >= new_right) {
  664. new_left = new_right - 1;
  665. }
  666. } else if (server->resize_edges & WLR_EDGE_RIGHT) {
  667. new_right = (int) border_x;
  668. if (new_right <= new_left) {
  669. new_right = new_left + 1;
  670. }
  671. }
  672. int new_width = new_right - new_left;
  673. int new_height = new_bottom - new_top;
  674. if (view_should_decorate(view)) {
  675. int top, left, right, bottom;
  676. view_deco_insets(view, &top, &left, &right, &bottom);
  677. view_place_visible_topleft(view, new_left - left, new_top - top);
  678. if (view->is_xwayland && view->xsurface != NULL) {
  679. wlr_xwayland_surface_configure(view->xsurface,
  680. (int16_t) new_left, (int16_t) new_top,
  681. (uint16_t) new_width, (uint16_t) new_height);
  682. } else {
  683. wlr_xdg_toplevel_set_size(view->xdg_toplevel, new_width, new_height);
  684. }
  685. return;
  686. }
  687. if (view->is_xwayland && view->xsurface != NULL) {
  688. wlr_scene_node_set_position(&view->scene_tree->node, new_left, new_top);
  689. wlr_xwayland_surface_configure(view->xsurface,
  690. (int16_t) new_left, (int16_t) new_top,
  691. (uint16_t) new_width, (uint16_t) new_height);
  692. } else {
  693. struct wlr_box geo_box;
  694. wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo_box);
  695. wlr_scene_node_set_position(&view->scene_tree->node,
  696. new_left - geo_box.x, new_top - geo_box.y);
  697. wlr_xdg_toplevel_set_size(view->xdg_toplevel, new_width, new_height);
  698. }
  699. }
  700. /* 1 = focus-follows-mouse (focusable view under the pointer gets focus). The focus-clear
  701. * over the desktop is intentionally NOT done here: it made the dock reveal jam after a couple
  702. * of cycles. The dock autohide must instead be driven by pointer-leave on the shell side. */
  703. #define MJC_FOCUS_FOLLOWS_MOUSE 1
  704. static void process_cursor_motion(struct mjc_server *server, uint32_t time) {
  705. if (server->cursor_mode == MJC_CURSOR_MOVE) {
  706. process_cursor_move(server);
  707. return;
  708. } else if (server->cursor_mode == MJC_CURSOR_RESIZE) {
  709. process_cursor_resize(server);
  710. return;
  711. }
  712. double sx, sy;
  713. struct wlr_seat *seat = server->seat;
  714. struct wlr_surface *surface = NULL;
  715. struct mjc_view *view = desktop_view_at(server, server->cursor->x,
  716. server->cursor->y, &surface, &sx, &sy);
  717. /* server-side decorations own the pointer over the frame: refresh button hover
  718. * and pick the cursor (resize on edges/corners, hand over buttons, arrow over the
  719. * draggable titlebar). When the frame owns the pointer the client gets no focus. */
  720. deco_update_hover(server);
  721. struct mjc_view *rview = NULL;
  722. uint32_t redges = deco_resize_edges(server, server->cursor->x,
  723. server->cursor->y, &rview);
  724. bool deco_owns = false;
  725. const char *deco_cursor = NULL;
  726. if (redges != 0) {
  727. deco_cursor = deco_resize_cursor(redges);
  728. deco_owns = true;
  729. } else {
  730. struct mjc_view *tv;
  731. wl_list_for_each(tv, &server->views, link) {
  732. double tx, ty;
  733. int w;
  734. if (!deco_titlebar_box(tv, &tx, &ty, &w)) {
  735. continue;
  736. }
  737. if (server->cursor->x >= tx && server->cursor->x < tx + w &&
  738. server->cursor->y >= ty &&
  739. server->cursor->y < ty + MJC_DECO_TITLEBAR_H) {
  740. deco_cursor = (tv->hovered_button >= 0) ? "pointer" : "default";
  741. deco_owns = true;
  742. break;
  743. }
  744. }
  745. }
  746. if (deco_owns) {
  747. wlr_cursor_set_xcursor(server->cursor, server->cursor_mgr, deco_cursor);
  748. wlr_seat_pointer_clear_focus(seat);
  749. } else if (surface == NULL) {
  750. wlr_cursor_set_xcursor(server->cursor, server->cursor_mgr, "default");
  751. wlr_seat_pointer_clear_focus(seat);
  752. } else {
  753. wlr_seat_pointer_notify_enter(seat, surface, sx, sy);
  754. wlr_seat_pointer_notify_motion(seat, time, sx, sy);
  755. /* focus-follows-mouse: focusable view under the pointer gets keyboard focus (no
  756. * restack). Do NOT clear focus over non-focusable surfaces — that churns focus and
  757. * jams the dock reveal; autohide is handled by pointer-leave on the shell side. */
  758. if (MJC_FOCUS_FOLLOWS_MOUSE && view != NULL && view->focusable) {
  759. view_focus_internal(view, false);
  760. }
  761. }
  762. // broadcast the pointer position to ipc clients (throttled), so the shell can do
  763. // edge detection without the AWT global pointer which is unreliable under nested XWayland
  764. if (server->cbs.pointer != NULL && (time - server->last_pointer_ms) >= 30u) {
  765. server->last_pointer_ms = time;
  766. server->cbs.pointer(server->ud,
  767. (int) server->cursor->x, (int) server->cursor->y);
  768. }
  769. }
  770. static void server_cursor_motion(struct wl_listener *listener, void *data) {
  771. struct mjc_server *server = wl_container_of(listener, server, cursor_motion);
  772. struct wlr_pointer_motion_event *event = data;
  773. wlr_cursor_move(server->cursor, &event->pointer->base,
  774. event->delta_x, event->delta_y);
  775. process_cursor_motion(server, event->time_msec);
  776. }
  777. static void server_cursor_motion_absolute(struct wl_listener *listener, void *data) {
  778. struct mjc_server *server =
  779. wl_container_of(listener, server, cursor_motion_absolute);
  780. struct wlr_pointer_motion_absolute_event *event = data;
  781. wlr_cursor_warp_absolute(server->cursor, &event->pointer->base,
  782. event->x, event->y);
  783. process_cursor_motion(server, event->time_msec);
  784. }
  785. static void server_cursor_button(struct wl_listener *listener, void *data) {
  786. struct mjc_server *server = wl_container_of(listener, server, cursor_button);
  787. struct wlr_pointer_button_event *event = data;
  788. wlr_seat_pointer_notify_button(server->seat,
  789. event->time_msec, event->button, event->state);
  790. if (event->state == WL_POINTER_BUTTON_STATE_RELEASED) {
  791. server->cursor_mode = MJC_CURSOR_PASSTHROUGH;
  792. server->grabbed_view = NULL;
  793. } else {
  794. /* a press over a window's titlebar/buttons is handled by the decoration
  795. * (focus + move grab, or a min/max/close action) and must not fall through
  796. * to client focus */
  797. if (deco_handle_button(server, server->cursor->x, server->cursor->y, true)) {
  798. return;
  799. }
  800. double sx, sy;
  801. struct wlr_surface *surface = NULL;
  802. struct mjc_view *view = desktop_view_at(server,
  803. server->cursor->x, server->cursor->y, &surface, &sx, &sy);
  804. if (view != NULL && view->focusable) {
  805. mjc_view_focus(view);
  806. }
  807. }
  808. }
  809. static void server_cursor_axis(struct wl_listener *listener, void *data) {
  810. struct mjc_server *server = wl_container_of(listener, server, cursor_axis);
  811. struct wlr_pointer_axis_event *event = data;
  812. wlr_seat_pointer_notify_axis(server->seat,
  813. event->time_msec, event->orientation, event->delta,
  814. event->delta_discrete, event->source, event->relative_direction);
  815. }
  816. static void server_cursor_frame(struct wl_listener *listener, void *data) {
  817. (void) data;
  818. struct mjc_server *server = wl_container_of(listener, server, cursor_frame);
  819. wlr_seat_pointer_notify_frame(server->seat);
  820. }
  821. /* ------------------------------------------------------------------ */
  822. /* interactive move/resize */
  823. /* ------------------------------------------------------------------ */
  824. static void view_current_geometry(struct mjc_view *view, struct wlr_box *box) {
  825. struct wlr_box frame;
  826. if (view_deco_frame_box(view, &frame)) {
  827. box->x = frame.x + MJC_DECO_BORDER;
  828. box->y = frame.y + MJC_DECO_TITLEBAR_H;
  829. box->width = frame.width - 2 * MJC_DECO_BORDER;
  830. box->height = frame.height - MJC_DECO_TITLEBAR_H - MJC_DECO_BORDER;
  831. return;
  832. }
  833. struct wlr_box local;
  834. if (view->scene_tree != NULL && view_client_surface_box(view, &local)) {
  835. box->x = view->scene_tree->node.x + local.x;
  836. box->y = view->scene_tree->node.y + local.y;
  837. box->width = local.width;
  838. box->height = local.height;
  839. return;
  840. }
  841. if (view->is_xwayland && view->xsurface != NULL) {
  842. box->x = view->scene_tree != NULL ? view->scene_tree->node.x : view->xsurface->x;
  843. box->y = view->scene_tree != NULL ? view->scene_tree->node.y : view->xsurface->y;
  844. box->width = view->xsurface->width;
  845. box->height = view->xsurface->height;
  846. } else if (view->xdg_toplevel != NULL && view->scene_tree != NULL) {
  847. struct wlr_box geo;
  848. wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo);
  849. box->x = view->scene_tree->node.x + geo.x;
  850. box->y = view->scene_tree->node.y + geo.y;
  851. box->width = geo.width;
  852. box->height = geo.height;
  853. } else {
  854. box->x = box->y = box->width = box->height = 0;
  855. }
  856. }
  857. /* extra pixels the compositor draws around the client content (titlebar + borders) */
  858. static void view_deco_insets(struct mjc_view *view,
  859. int *top, int *left, int *right, int *bottom) {
  860. if (view_should_decorate(view)) {
  861. *top = MJC_DECO_TITLEBAR_H;
  862. *left = MJC_DECO_BORDER;
  863. *right = MJC_DECO_BORDER;
  864. *bottom = MJC_DECO_BORDER;
  865. } else {
  866. *top = *left = *right = *bottom = 0;
  867. }
  868. }
  869. /* outer frame bounds in layout space */
  870. static void view_visible_geometry(struct mjc_view *view, struct wlr_box *box) {
  871. if (view_deco_frame_box(view, box)) {
  872. return;
  873. }
  874. view_current_geometry(view, box);
  875. }
  876. /* place the outer frame's top-left at (vx, vy) in layout space */
  877. static void view_place_visible_topleft(struct mjc_view *view, int vx, int vy) {
  878. if (view_should_decorate(view) && view->scene_tree != NULL) {
  879. struct wlr_box frame;
  880. if (view_deco_frame_box(view, &frame)) {
  881. view_clamp_frame_pos(view->server, &vx, &vy, frame.width, frame.height);
  882. }
  883. wlr_scene_node_set_position(&view->scene_tree->node, vx, vy);
  884. if (view->decorated) {
  885. view_deco_snap_client(view);
  886. view_decoration_update(view);
  887. }
  888. return;
  889. }
  890. int top, left, right, bottom;
  891. view_deco_insets(view, &top, &left, &right, &bottom);
  892. int cx = vx + left;
  893. int cy = vy + top;
  894. if (view->is_xwayland) {
  895. mjc_view_set_position(view, cx, cy);
  896. } else {
  897. struct wlr_box off;
  898. wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &off);
  899. wlr_scene_node_set_position(&view->scene_tree->node, cx - off.x, cy - off.y);
  900. }
  901. }
  902. /* Client content width/height — prefer xdg geometry (excludes CSD shadow margins). */
  903. static bool view_deco_content_metrics(struct mjc_view *view, int *cw, int *ch) {
  904. if (!view->is_xwayland && view->xdg_toplevel != NULL) {
  905. struct wlr_box geo;
  906. wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo);
  907. if (geo.width > 0 && geo.height > 0) {
  908. *cw = geo.width;
  909. *ch = geo.height;
  910. return true;
  911. }
  912. }
  913. struct wlr_box local;
  914. if (view_client_surface_box(view, &local) && local.width > 0 && local.height > 0) {
  915. *cw = local.width;
  916. *ch = local.height;
  917. return true;
  918. }
  919. if (view->is_xwayland && view->xsurface != NULL &&
  920. view->xsurface->width > 0 && view->xsurface->height > 0) {
  921. *cw = view->xsurface->width;
  922. *ch = view->xsurface->height;
  923. return true;
  924. }
  925. return false;
  926. }
  927. /* Snap the client surface into the content slot under our titlebar. */
  928. static void view_deco_snap_client(struct mjc_view *view) {
  929. struct wlr_surface *target = view_surface(view);
  930. if (target == NULL || view->scene_tree == NULL) {
  931. return;
  932. }
  933. struct wlr_scene_buffer *buf =
  934. find_buffer_for_surface(&view->scene_tree->node, target);
  935. if (buf == NULL) {
  936. return;
  937. }
  938. int ox = 0, oy = 0;
  939. if (!view->is_xwayland && view->xdg_toplevel != NULL) {
  940. struct wlr_box geo;
  941. wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo);
  942. ox = geo.x;
  943. oy = geo.y;
  944. } else {
  945. view_geo_offset(view, &ox, &oy);
  946. }
  947. wlr_scene_node_set_position(&buf->node,
  948. MJC_DECO_BORDER - ox, MJC_DECO_TITLEBAR_H - oy);
  949. if (view->deco_titlebar != NULL) {
  950. wlr_scene_node_raise_to_top(&view->deco_titlebar->node);
  951. }
  952. }
  953. /* Outer server-side frame in layout coordinates. */
  954. static bool view_deco_frame_box(struct mjc_view *view, struct wlr_box *frame) {
  955. if (!view_should_decorate(view) || view->scene_tree == NULL) {
  956. return false;
  957. }
  958. int cw, ch;
  959. if (!view_deco_content_metrics(view, &cw, &ch)) {
  960. if (view->xdg_toplevel != NULL) {
  961. struct wlr_box geo;
  962. wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo);
  963. cw = geo.width;
  964. ch = geo.height;
  965. } else if (view->is_xwayland && view->xsurface != NULL) {
  966. cw = view->xsurface->width;
  967. ch = view->xsurface->height;
  968. }
  969. if (cw <= 0 || ch <= 0) {
  970. return false;
  971. }
  972. }
  973. frame->x = view->scene_tree->node.x;
  974. frame->y = view->scene_tree->node.y;
  975. frame->width = cw + 2 * MJC_DECO_BORDER;
  976. frame->height = MJC_DECO_TITLEBAR_H + ch + MJC_DECO_BORDER;
  977. return true;
  978. }
  979. static void begin_interactive(struct mjc_view *view,
  980. enum mjc_cursor_mode mode, uint32_t edges) {
  981. struct mjc_server *server = view->server;
  982. if (view_surface(view) !=
  983. server->seat->pointer_state.focused_surface) {
  984. /* only react to requests from the focused client */
  985. return;
  986. }
  987. server->grabbed_view = view;
  988. server->cursor_mode = mode;
  989. struct wlr_box geo_box;
  990. view_current_geometry(view, &geo_box);
  991. if (mode == MJC_CURSOR_MOVE) {
  992. server->grab_x = server->cursor->x - view->scene_tree->node.x;
  993. server->grab_y = server->cursor->y - view->scene_tree->node.y;
  994. } else {
  995. double border_x = geo_box.x +
  996. ((edges & WLR_EDGE_RIGHT) ? geo_box.width : 0);
  997. double border_y = geo_box.y +
  998. ((edges & WLR_EDGE_BOTTOM) ? geo_box.height : 0);
  999. server->grab_x = server->cursor->x - border_x;
  1000. server->grab_y = server->cursor->y - border_y;
  1001. server->grab_geobox = geo_box;
  1002. server->resize_edges = edges;
  1003. }
  1004. }
  1005. /* ------------------------------------------------------------------ */
  1006. /* server-side decorations */
  1007. /* ------------------------------------------------------------------ */
  1008. /* a wlr_buffer backed by a cairo ARGB32 image surface. cairo ARGB32 is
  1009. * premultiplied and laid out as B,G,R,A on little-endian, which matches
  1010. * DRM_FORMAT_ARGB8888 — so the scene can upload it directly as a texture. */
  1011. struct mjc_cairo_buffer {
  1012. struct wlr_buffer base;
  1013. cairo_surface_t *surface;
  1014. };
  1015. static void cairo_buffer_destroy(struct wlr_buffer *wlr_buffer) {
  1016. struct mjc_cairo_buffer *buf = wl_container_of(wlr_buffer, buf, base);
  1017. cairo_surface_destroy(buf->surface);
  1018. free(buf);
  1019. }
  1020. static bool cairo_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer,
  1021. uint32_t flags, void **data, uint32_t *format, size_t *stride) {
  1022. struct mjc_cairo_buffer *buf = wl_container_of(wlr_buffer, buf, base);
  1023. if (flags & WLR_BUFFER_DATA_PTR_ACCESS_WRITE) {
  1024. return false;
  1025. }
  1026. *data = cairo_image_surface_get_data(buf->surface);
  1027. *format = DRM_FORMAT_ARGB8888;
  1028. *stride = (size_t) cairo_image_surface_get_stride(buf->surface);
  1029. return true;
  1030. }
  1031. static void cairo_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) {
  1032. (void) wlr_buffer;
  1033. }
  1034. static const struct wlr_buffer_impl cairo_buffer_impl = {
  1035. .destroy = cairo_buffer_destroy,
  1036. .begin_data_ptr_access = cairo_buffer_begin_data_ptr_access,
  1037. .end_data_ptr_access = cairo_buffer_end_data_ptr_access,
  1038. };
  1039. static struct mjc_cairo_buffer *cairo_buffer_create(int width, int height) {
  1040. struct mjc_cairo_buffer *buf = calloc(1, sizeof(*buf));
  1041. if (buf == NULL) {
  1042. return NULL;
  1043. }
  1044. buf->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
  1045. if (cairo_surface_status(buf->surface) != CAIRO_STATUS_SUCCESS) {
  1046. cairo_surface_destroy(buf->surface);
  1047. free(buf);
  1048. return NULL;
  1049. }
  1050. wlr_buffer_init(&buf->base, &cairo_buffer_impl, width, height);
  1051. return buf;
  1052. }
  1053. /* content-surface offset inside the view's scene_tree (from the scene graph, not
  1054. * stale xdg geometry — GTK/libadwaita can keep CSD margins until after commit) */
  1055. static void view_geo_offset(struct mjc_view *view, int *ox, int *oy) {
  1056. struct wlr_box local;
  1057. if (view_client_surface_box(view, &local)) {
  1058. *ox = local.x;
  1059. *oy = local.y;
  1060. return;
  1061. }
  1062. if (view->is_xwayland) {
  1063. *ox = 0;
  1064. *oy = 0;
  1065. } else if (view->xdg_toplevel != NULL) {
  1066. struct wlr_box geo;
  1067. wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo);
  1068. *ox = geo.x;
  1069. *oy = geo.y;
  1070. } else {
  1071. *ox = *oy = 0;
  1072. }
  1073. }
  1074. static void view_content_size(struct mjc_view *view, int *w, int *h) {
  1075. if (!view_deco_content_metrics(view, w, h)) {
  1076. struct wlr_box box;
  1077. view_current_geometry(view, &box);
  1078. *w = box.width;
  1079. *h = box.height;
  1080. }
  1081. }
  1082. /* center x of titlebar button i (0=min, 1=max, 2=close), right-aligned GNOME order */
  1083. static int deco_button_cx(int width, int i) {
  1084. int step = 2 * MJC_DECO_BTN_R + MJC_DECO_BTN_GAP;
  1085. int close_cx = width - MJC_DECO_PAD - MJC_DECO_BTN_R;
  1086. return close_cx - (2 - i) * step;
  1087. }
  1088. static bool view_should_decorate(struct mjc_view *view) {
  1089. if (!view->mapped || view->scene_tree == NULL) {
  1090. return false;
  1091. }
  1092. /* chrome (shell wallpaper/panels/menus) and chromeless windows get no frame;
  1093. * everything else is a normal app window and is decorated in any layer, so
  1094. * always-on-top / always-on-bottom apps keep their titlebar */
  1095. if (view->is_chrome || view->deco_disabled) {
  1096. return false;
  1097. }
  1098. if (view->is_xwayland && view->xsurface != NULL &&
  1099. view->xsurface->override_redirect) {
  1100. return false;
  1101. }
  1102. return true;
  1103. }
  1104. /* a path with rounded top corners and square bottom corners */
  1105. static void deco_rounded_top(cairo_t *cr, double x, double y,
  1106. double w, double h, double r) {
  1107. cairo_new_sub_path(cr);
  1108. cairo_arc(cr, x + r, y + r, r, M_PI, 1.5 * M_PI);
  1109. cairo_arc(cr, x + w - r, y + r, r, 1.5 * M_PI, 2.0 * M_PI);
  1110. cairo_line_to(cr, x + w, y + h);
  1111. cairo_line_to(cr, x, y + h);
  1112. cairo_close_path(cr);
  1113. }
  1114. /* borderColor-tinted frame edges with alpha so the desktop shows through */
  1115. static void deco_border_color_rgba(const struct mjc_server *server, float rgba[4]) {
  1116. rgba[0] = server->deco_icon_fg[0];
  1117. rgba[1] = server->deco_icon_fg[1];
  1118. rgba[2] = server->deco_icon_fg[2];
  1119. rgba[3] = MJC_DECO_BORDER_A;
  1120. }
  1121. /* dock / BlurPanel-style glass fill for the titlebar bitmap */
  1122. static void deco_paint_glass_titlebar(cairo_t *cr, int width, int height,
  1123. const float *bg, bool active) {
  1124. double scale = active ? 1.0 : 0.88;
  1125. cairo_save(cr);
  1126. deco_rounded_top(cr, 0, 0, width, height, MJC_DECO_RADIUS);
  1127. cairo_clip(cr);
  1128. cairo_pattern_t *pat = cairo_pattern_create_linear(0, 0, 0, height);
  1129. cairo_pattern_add_color_stop_rgba(pat, 0.0,
  1130. bg[0], bg[1], bg[2], MJC_DECO_GLASS_A0 * scale);
  1131. cairo_pattern_add_color_stop_rgba(pat, 0.5,
  1132. bg[0], bg[1], bg[2], MJC_DECO_GLASS_A1 * scale);
  1133. cairo_pattern_add_color_stop_rgba(pat, 1.0,
  1134. bg[0], bg[1], bg[2], MJC_DECO_GLASS_A2 * scale);
  1135. cairo_set_source(cr, pat);
  1136. cairo_paint(cr);
  1137. cairo_pattern_destroy(pat);
  1138. /* white sheen overlay (BlurPanel linearGradient 0x66.. -> 0x1A..) */
  1139. pat = cairo_pattern_create_linear(0, 0, 0, height);
  1140. cairo_pattern_add_color_stop_rgba(pat, 0.0, 1, 1, 1, MJC_DECO_GLASS_SHEEN0 * scale);
  1141. cairo_pattern_add_color_stop_rgba(pat, 1.0, 1, 1, 1, MJC_DECO_GLASS_SHEEN1 * scale);
  1142. cairo_set_source(cr, pat);
  1143. cairo_paint(cr);
  1144. cairo_pattern_destroy(pat);
  1145. cairo_restore(cr);
  1146. }
  1147. /* render the titlebar of `width` px into a fresh cairo buffer and hand it to the
  1148. * scene node (the scene takes a ref; we drop ours) */
  1149. static void deco_render(struct mjc_view *view, int width) {
  1150. if (view->deco_titlebar == NULL || width <= 0) {
  1151. return;
  1152. }
  1153. struct mjc_server *server = view->server;
  1154. int h = MJC_DECO_TITLEBAR_H;
  1155. struct mjc_cairo_buffer *buf = cairo_buffer_create(width, h);
  1156. if (buf == NULL) {
  1157. return;
  1158. }
  1159. cairo_t *cr = cairo_create(buf->surface);
  1160. cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
  1161. cairo_paint(cr);
  1162. cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
  1163. const float *bg = server->deco_bg;
  1164. const float *fg = server->deco_fg;
  1165. const float *icon_bg = server->deco_icon_bg;
  1166. const float *icon_fg = server->deco_icon_fg;
  1167. bool active = (server->focused_view == view);
  1168. /* semi-transparent glass titlebar — wallpaper / desktop bleeds through like dock */
  1169. deco_paint_glass_titlebar(cr, width, h, bg, active);
  1170. /* hairline top highlight for depth */
  1171. cairo_set_source_rgba(cr, 1, 1, 1, active ? 0.10 : 0.05);
  1172. cairo_set_line_width(cr, 1.0);
  1173. deco_rounded_top(cr, 0.5, 0.5, width - 1, h, MJC_DECO_RADIUS);
  1174. cairo_stroke(cr);
  1175. /* title text (cairo toy font api, no pango dependency) */
  1176. const char *title = mjc_view_title(view);
  1177. if (title != NULL && title[0] != '\0') {
  1178. cairo_select_font_face(cr, "Sans",
  1179. CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
  1180. cairo_set_font_size(cr, 13.0);
  1181. cairo_font_extents_t fe;
  1182. cairo_font_extents(cr, &fe);
  1183. double clip_r = deco_button_cx(width, 0) - MJC_DECO_BTN_R - 8;
  1184. cairo_save(cr);
  1185. cairo_rectangle(cr, 0, 0, clip_r > 0 ? clip_r : 0, h);
  1186. cairo_clip(cr);
  1187. cairo_set_source_rgba(cr, fg[0], fg[1], fg[2], active ? 0.95 : 0.6);
  1188. cairo_move_to(cr, MJC_DECO_PAD, (h + fe.ascent - fe.descent) / 2.0);
  1189. cairo_show_text(cr, title);
  1190. cairo_restore(cr);
  1191. }
  1192. /* titlebar buttons: same palette roles as dock / control center icons
  1193. * (circle = iconsTintColor, glyph = borderColor) */
  1194. double cy = h / 2.0;
  1195. for (int i = 0; i < 3; i++) {
  1196. double cx = deco_button_cx(width, i);
  1197. bool hov = (view->hovered_button == i);
  1198. double circle_a = hov ? 1.0 : (active ? 0.85 : 0.65);
  1199. cairo_set_source_rgba(cr,
  1200. icon_bg[0], icon_bg[1], icon_bg[2], circle_a);
  1201. cairo_arc(cr, cx, cy, MJC_DECO_BTN_R, 0, 2 * M_PI);
  1202. cairo_fill(cr);
  1203. cairo_set_source_rgba(cr,
  1204. icon_fg[0], icon_fg[1], icon_fg[2], hov ? 1.0 : (active ? 0.95 : 0.7));
  1205. cairo_set_line_width(cr, 1.4);
  1206. double g = 4.0;
  1207. if (i == 0) { /* minimize: bottom dash */
  1208. cairo_move_to(cr, cx - g, cy + 2.5);
  1209. cairo_line_to(cr, cx + g, cy + 2.5);
  1210. cairo_stroke(cr);
  1211. } else if (i == 1) { /* maximize: square */
  1212. cairo_rectangle(cr, cx - g, cy - g, 2 * g, 2 * g);
  1213. cairo_stroke(cr);
  1214. } else { /* close: x */
  1215. cairo_move_to(cr, cx - g, cy - g);
  1216. cairo_line_to(cr, cx + g, cy + g);
  1217. cairo_move_to(cr, cx + g, cy - g);
  1218. cairo_line_to(cr, cx - g, cy + g);
  1219. cairo_stroke(cr);
  1220. }
  1221. }
  1222. cairo_destroy(cr);
  1223. cairo_surface_flush(buf->surface);
  1224. wlr_scene_buffer_set_buffer(view->deco_titlebar, &buf->base);
  1225. wlr_buffer_drop(&buf->base);
  1226. }
  1227. static void deco_repaint(struct mjc_view *view) {
  1228. if (view->decorated && view->deco_w > 0) {
  1229. deco_render(view, view->deco_w);
  1230. }
  1231. }
  1232. static void view_decoration_destroy(struct mjc_view *view) {
  1233. if (view->deco_titlebar != NULL) {
  1234. wlr_scene_node_destroy(&view->deco_titlebar->node);
  1235. view->deco_titlebar = NULL;
  1236. }
  1237. for (int i = 0; i < 3; i++) {
  1238. if (view->deco_border[i] != NULL) {
  1239. wlr_scene_node_destroy(&view->deco_border[i]->node);
  1240. view->deco_border[i] = NULL;
  1241. }
  1242. }
  1243. view->decorated = false;
  1244. view->deco_w = 0;
  1245. view->deco_h = 0;
  1246. view->hovered_button = -1;
  1247. }
  1248. /* create/refresh/remove a window's decoration to match its current state */
  1249. static void view_decoration_update(struct mjc_view *view) {
  1250. if (!view_should_decorate(view)) {
  1251. view_decoration_destroy(view);
  1252. return;
  1253. }
  1254. int cw, ch;
  1255. if (!view_deco_content_metrics(view, &cw, &ch)) {
  1256. return;
  1257. }
  1258. struct mjc_server *server = view->server;
  1259. int tb = MJC_DECO_TITLEBAR_H;
  1260. int b = MJC_DECO_BORDER;
  1261. int frame_w = cw + 2 * b;
  1262. if (view->deco_titlebar == NULL) {
  1263. view->deco_titlebar = wlr_scene_buffer_create(view->scene_tree, NULL);
  1264. float border_col[4];
  1265. deco_border_color_rgba(server, border_col);
  1266. for (int i = 0; i < 3; i++) {
  1267. view->deco_border[i] =
  1268. wlr_scene_rect_create(view->scene_tree, 1, 1, border_col);
  1269. }
  1270. view->hovered_button = -1;
  1271. view->decorated = true;
  1272. view->deco_w = 0;
  1273. }
  1274. view_deco_snap_client(view);
  1275. if (frame_w != view->deco_w) {
  1276. deco_render(view, frame_w);
  1277. }
  1278. view->deco_w = frame_w;
  1279. view->deco_h = ch;
  1280. /* fixed SSD layout inside scene_tree: titlebar at (0,0), content at (b,tb) */
  1281. wlr_scene_node_set_position(&view->deco_titlebar->node, 0, 0);
  1282. wlr_scene_rect_set_size(view->deco_border[0], b, tb + ch);
  1283. wlr_scene_node_set_position(&view->deco_border[0]->node, 0, 0);
  1284. wlr_scene_rect_set_size(view->deco_border[1], b, tb + ch);
  1285. wlr_scene_node_set_position(&view->deco_border[1]->node, b + cw, 0);
  1286. wlr_scene_rect_set_size(view->deco_border[2], frame_w, b);
  1287. wlr_scene_node_set_position(&view->deco_border[2]->node, 0, tb + ch);
  1288. }
  1289. /* layout-space top-left and size of a decorated view's titlebar */
  1290. static bool deco_titlebar_box(struct mjc_view *view,
  1291. double *tx, double *ty, int *w) {
  1292. if (!view->decorated || view->deco_titlebar == NULL || view->scene_tree == NULL) {
  1293. return false;
  1294. }
  1295. *tx = view->scene_tree->node.x;
  1296. *ty = view->scene_tree->node.y;
  1297. *w = view->deco_w > 0 ? view->deco_w : 0;
  1298. return *w > 0;
  1299. }
  1300. /* button index (0..2) under (lx,ly), or -1 if over the titlebar but not a button */
  1301. static int deco_button_index(double tx, double ty, int w, double lx, double ly) {
  1302. double cy = ty + MJC_DECO_TITLEBAR_H / 2.0;
  1303. for (int i = 0; i < 3; i++) {
  1304. double cx = tx + deco_button_cx(w, i);
  1305. double dx = lx - cx, dy = ly - cy;
  1306. double rr = (MJC_DECO_BTN_R + 2) * (MJC_DECO_BTN_R + 2);
  1307. if (dx * dx + dy * dy <= rr) {
  1308. return i;
  1309. }
  1310. }
  1311. return -1;
  1312. }
  1313. /* resize-edge mask under (lx,ly) for the first decorated, non-maximized window
  1314. * whose frame border zone the pointer is in; 0 when the pointer is not on an edge */
  1315. static uint32_t deco_resize_edges(struct mjc_server *server,
  1316. double lx, double ly, struct mjc_view **out) {
  1317. struct mjc_view *view;
  1318. wl_list_for_each(view, &server->views, link) {
  1319. if (!view->decorated || view->scene_tree == NULL || view->maximized) {
  1320. continue;
  1321. }
  1322. struct wlr_box frame;
  1323. if (!view_deco_frame_box(view, &frame)) {
  1324. continue;
  1325. }
  1326. double x0 = frame.x;
  1327. double y0 = frame.y;
  1328. double x1 = frame.x + frame.width;
  1329. double y1 = frame.y + frame.height;
  1330. double r = MJC_DECO_RESIZE;
  1331. if (lx < x0 - r || lx > x1 + r || ly < y0 - r || ly > y1 + r) {
  1332. continue;
  1333. }
  1334. uint32_t edges = 0;
  1335. if (ly <= y0 + r) {
  1336. edges |= WLR_EDGE_TOP;
  1337. } else if (ly >= y1 - r) {
  1338. edges |= WLR_EDGE_BOTTOM;
  1339. }
  1340. if (lx <= x0 + r) {
  1341. edges |= WLR_EDGE_LEFT;
  1342. } else if (lx >= x1 - r) {
  1343. edges |= WLR_EDGE_RIGHT;
  1344. }
  1345. if (edges != 0) {
  1346. if (out != NULL) {
  1347. *out = view;
  1348. }
  1349. return edges;
  1350. }
  1351. }
  1352. return 0;
  1353. }
  1354. static const char *deco_resize_cursor(uint32_t edges) {
  1355. bool top = edges & WLR_EDGE_TOP, bottom = edges & WLR_EDGE_BOTTOM;
  1356. bool left = edges & WLR_EDGE_LEFT, right = edges & WLR_EDGE_RIGHT;
  1357. if (top && left) return "nw-resize";
  1358. if (top && right) return "ne-resize";
  1359. if (bottom && left) return "sw-resize";
  1360. if (bottom && right) return "se-resize";
  1361. if (top) return "n-resize";
  1362. if (bottom) return "s-resize";
  1363. if (left) return "w-resize";
  1364. if (right) return "e-resize";
  1365. return "default";
  1366. }
  1367. static void deco_update_hover(struct mjc_server *server) {
  1368. struct mjc_view *view;
  1369. wl_list_for_each(view, &server->views, link) {
  1370. double tx, ty;
  1371. int w;
  1372. if (!deco_titlebar_box(view, &tx, &ty, &w)) {
  1373. continue;
  1374. }
  1375. int want = -1;
  1376. if (server->cursor->x >= tx && server->cursor->x < tx + w &&
  1377. server->cursor->y >= ty &&
  1378. server->cursor->y < ty + MJC_DECO_TITLEBAR_H) {
  1379. want = deco_button_index(tx, ty, w, server->cursor->x, server->cursor->y);
  1380. }
  1381. if (want != view->hovered_button) {
  1382. view->hovered_button = want;
  1383. deco_repaint(view);
  1384. }
  1385. }
  1386. }
  1387. static bool deco_handle_button(struct mjc_server *server,
  1388. double lx, double ly, bool pressed) {
  1389. if (!pressed) {
  1390. return false;
  1391. }
  1392. /* edge/corner of a window frame => start an interactive resize. Mirrors the
  1393. * RESIZE branch of begin_interactive but without the focused-surface guard
  1394. * (the pointer is over the decoration, not the client surface). */
  1395. struct mjc_view *rv = NULL;
  1396. uint32_t redges = deco_resize_edges(server, lx, ly, &rv);
  1397. if (redges != 0 && rv != NULL) {
  1398. if (rv->focusable) {
  1399. mjc_view_focus(rv);
  1400. }
  1401. struct wlr_box geo_box;
  1402. view_current_geometry(rv, &geo_box);
  1403. double border_x = geo_box.x +
  1404. ((redges & WLR_EDGE_RIGHT) ? geo_box.width : 0);
  1405. double border_y = geo_box.y +
  1406. ((redges & WLR_EDGE_BOTTOM) ? geo_box.height : 0);
  1407. server->grabbed_view = rv;
  1408. server->cursor_mode = MJC_CURSOR_RESIZE;
  1409. server->grab_x = server->cursor->x - border_x;
  1410. server->grab_y = server->cursor->y - border_y;
  1411. server->grab_geobox = geo_box;
  1412. server->resize_edges = redges;
  1413. return true;
  1414. }
  1415. struct mjc_view *view;
  1416. wl_list_for_each(view, &server->views, link) {
  1417. double tx, ty;
  1418. int w;
  1419. if (!deco_titlebar_box(view, &tx, &ty, &w)) {
  1420. continue;
  1421. }
  1422. if (lx < tx || lx >= tx + w || ly < ty ||
  1423. ly >= ty + MJC_DECO_TITLEBAR_H) {
  1424. continue;
  1425. }
  1426. int btn = deco_button_index(tx, ty, w, lx, ly);
  1427. if (view->focusable) {
  1428. mjc_view_focus(view);
  1429. }
  1430. if (btn == 0) {
  1431. mjc_view_set_minimized(view, true);
  1432. } else if (btn == 1) {
  1433. mjc_view_set_maximized(view, !view->maximized);
  1434. } else if (btn == 2) {
  1435. mjc_view_close(view);
  1436. } else {
  1437. /* drag the titlebar => interactive move (no focused-surface guard
  1438. * because the pointer is over the decoration, not the client) */
  1439. server->grabbed_view = view;
  1440. server->cursor_mode = MJC_CURSOR_MOVE;
  1441. server->grab_x = server->cursor->x - view->scene_tree->node.x;
  1442. server->grab_y = server->cursor->y - view->scene_tree->node.y;
  1443. }
  1444. return true;
  1445. }
  1446. return false;
  1447. }
  1448. /* ------------------------------------------------------------------ */
  1449. /* view common */
  1450. /* ------------------------------------------------------------------ */
  1451. /* places a freshly mapped floating view in the middle of the output */
  1452. static void view_center(struct mjc_view *view) {
  1453. struct mjc_server *server = view->server;
  1454. if (view->scene_tree == NULL || view->maximized) {
  1455. return;
  1456. }
  1457. struct wlr_box out;
  1458. wlr_output_layout_get_box(server->output_layout, NULL, &out);
  1459. struct wlr_box vis;
  1460. view_visible_geometry(view, &vis);
  1461. if (out.width <= 0 || vis.width <= 0) {
  1462. return;
  1463. }
  1464. int vx = out.x + (out.width - vis.width) / 2;
  1465. int vy = out.y + (out.height - vis.height) / 2;
  1466. view_clamp_frame_pos(server, &vx, &vy, vis.width, vis.height);
  1467. view_place_visible_topleft(view, vx, vy);
  1468. }
  1469. static void view_emit_new(struct mjc_view *view) {
  1470. if (view->server->cbs.view_new != NULL) {
  1471. view->server->cbs.view_new(view->server->ud, view, view->is_xwayland);
  1472. }
  1473. }
  1474. static void view_emit_map(struct mjc_view *view) {
  1475. if (view->server->cbs.view_map != NULL) {
  1476. view->server->cbs.view_map(view->server->ud, view);
  1477. }
  1478. }
  1479. static void view_emit_unmap(struct mjc_view *view) {
  1480. if (view->server->cbs.view_unmap != NULL) {
  1481. view->server->cbs.view_unmap(view->server->ud, view);
  1482. }
  1483. }
  1484. static void view_emit_destroy(struct mjc_view *view) {
  1485. if (view->server->cbs.view_destroy != NULL) {
  1486. view->server->cbs.view_destroy(view->server->ud, view);
  1487. }
  1488. }
  1489. static struct mjc_view *view_create(struct mjc_server *server, bool is_xwayland) {
  1490. struct mjc_view *view = calloc(1, sizeof(*view));
  1491. view->server = server;
  1492. view->id = ++server->next_view_id;
  1493. view->is_xwayland = is_xwayland;
  1494. view->focusable = true;
  1495. view->layer = MJC_LAYER_NORMAL;
  1496. wl_list_insert(&server->views, &view->link);
  1497. return view;
  1498. }
  1499. static void view_free(struct mjc_view *view) {
  1500. clear_focus_if(view->server, view);
  1501. view_emit_destroy(view);
  1502. wl_list_remove(&view->link);
  1503. free(view);
  1504. }
  1505. /* ------------------------------------------------------------------ */
  1506. /* xdg shell views */
  1507. /* ------------------------------------------------------------------ */
  1508. static void xdg_toplevel_map(struct wl_listener *listener, void *data) {
  1509. (void) data;
  1510. struct mjc_view *view = wl_container_of(listener, view, map);
  1511. view->mapped = true;
  1512. view_emit_map(view); /* policy assigns chrome/layer before we frame the window */
  1513. view_decoration_update(view);
  1514. if (!view->placement_set) {
  1515. view->pending_center = true;
  1516. }
  1517. mjc_view_focus(view);
  1518. }
  1519. static void xdg_toplevel_unmap(struct wl_listener *listener, void *data) {
  1520. (void) data;
  1521. struct mjc_view *view = wl_container_of(listener, view, unmap);
  1522. view->mapped = false;
  1523. clear_focus_if(view->server, view);
  1524. view_decoration_destroy(view);
  1525. view_emit_unmap(view);
  1526. }
  1527. static void xdg_toplevel_commit(struct wl_listener *listener, void *data) {
  1528. (void) data;
  1529. struct mjc_view *view = wl_container_of(listener, view, commit);
  1530. if (view->xdg_toplevel->base->initial_commit) {
  1531. if (view->xdg_toplevel->requested.maximized) {
  1532. mjc_view_set_maximized(view, true);
  1533. } else {
  1534. /* 0x0 lets the client pick its own (or remembered) size */
  1535. wlr_xdg_toplevel_set_size(view->xdg_toplevel, 0, 0);
  1536. }
  1537. return;
  1538. }
  1539. if (view->pending_center && view->mapped && !view->maximized &&
  1540. !view->xdg_toplevel->current.maximized) {
  1541. struct wlr_box geo;
  1542. wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo);
  1543. if (geo.width > 0) {
  1544. view->pending_center = false;
  1545. if (view->saved_geo.width > 0 && view->scene_tree != NULL) {
  1546. view_place_visible_topleft(view,
  1547. view->saved_geo.x, view->saved_geo.y);
  1548. } else {
  1549. view_center(view);
  1550. }
  1551. }
  1552. }
  1553. /* keep the frame sized/placed to the (possibly just-resized) window */
  1554. if (view->decorated || view_should_decorate(view)) {
  1555. view_decoration_update(view);
  1556. }
  1557. }
  1558. static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) {
  1559. (void) data;
  1560. struct mjc_view *view = wl_container_of(listener, view, destroy);
  1561. wl_list_remove(&view->map.link);
  1562. wl_list_remove(&view->unmap.link);
  1563. wl_list_remove(&view->commit.link);
  1564. wl_list_remove(&view->destroy.link);
  1565. wl_list_remove(&view->request_move.link);
  1566. wl_list_remove(&view->request_resize.link);
  1567. wl_list_remove(&view->request_maximize.link);
  1568. wl_list_remove(&view->request_minimize.link);
  1569. wl_list_remove(&view->request_fullscreen.link);
  1570. wl_list_remove(&view->set_title.link);
  1571. wl_list_remove(&view->set_app_id.link);
  1572. view_free(view);
  1573. }
  1574. static void xdg_toplevel_request_move(struct wl_listener *listener, void *data) {
  1575. (void) data;
  1576. struct mjc_view *view = wl_container_of(listener, view, request_move);
  1577. begin_interactive(view, MJC_CURSOR_MOVE, 0);
  1578. }
  1579. static void xdg_toplevel_request_resize(struct wl_listener *listener, void *data) {
  1580. struct wlr_xdg_toplevel_resize_event *event = data;
  1581. struct mjc_view *view = wl_container_of(listener, view, request_resize);
  1582. begin_interactive(view, MJC_CURSOR_RESIZE, event->edges);
  1583. }
  1584. static void xdg_toplevel_request_maximize(struct wl_listener *listener, void *data) {
  1585. (void) data;
  1586. struct mjc_view *view = wl_container_of(listener, view, request_maximize);
  1587. if (view->xdg_toplevel->base->initialized) {
  1588. mjc_view_set_maximized(view, view->xdg_toplevel->requested.maximized);
  1589. wlr_xdg_surface_schedule_configure(view->xdg_toplevel->base);
  1590. }
  1591. }
  1592. static void xdg_toplevel_request_minimize(struct wl_listener *listener, void *data) {
  1593. (void) data;
  1594. struct mjc_view *view = wl_container_of(listener, view, request_minimize);
  1595. mjc_view_set_minimized(view, true);
  1596. }
  1597. static void xdg_toplevel_request_fullscreen(struct wl_listener *listener, void *data) {
  1598. (void) data;
  1599. struct mjc_view *view = wl_container_of(listener, view, request_fullscreen);
  1600. if (view->xdg_toplevel->base->initialized) {
  1601. wlr_xdg_surface_schedule_configure(view->xdg_toplevel->base);
  1602. }
  1603. }
  1604. static void xdg_toplevel_set_title(struct wl_listener *listener, void *data) {
  1605. (void) data;
  1606. struct mjc_view *view = wl_container_of(listener, view, set_title);
  1607. deco_repaint(view);
  1608. if (view->server->cbs.view_title != NULL) {
  1609. view->server->cbs.view_title(view->server->ud, view,
  1610. view->xdg_toplevel->title);
  1611. }
  1612. }
  1613. static void xdg_toplevel_set_app_id(struct wl_listener *listener, void *data) {
  1614. (void) data;
  1615. struct mjc_view *view = wl_container_of(listener, view, set_app_id);
  1616. if (view->server->cbs.view_app_id != NULL) {
  1617. view->server->cbs.view_app_id(view->server->ud, view,
  1618. view->xdg_toplevel->app_id);
  1619. }
  1620. }
  1621. static void server_new_xdg_toplevel(struct wl_listener *listener, void *data) {
  1622. struct mjc_server *server =
  1623. wl_container_of(listener, server, new_xdg_toplevel);
  1624. struct wlr_xdg_toplevel *xdg_toplevel = data;
  1625. struct mjc_view *view = view_create(server, false);
  1626. view->xdg_toplevel = xdg_toplevel;
  1627. /* App windows get our server-side frame by default. Clients that explicitly
  1628. * demand client-side decorations (xdg-decoration) are left chromeless in
  1629. * decoration_request_mode; shell surfaces are marked chrome by Policy. */
  1630. view->scene_tree = wlr_scene_xdg_surface_create(
  1631. server->layers[MJC_LAYER_NORMAL], xdg_toplevel->base);
  1632. view->scene_tree->node.data = view;
  1633. xdg_toplevel->base->data = view->scene_tree;
  1634. view->map.notify = xdg_toplevel_map;
  1635. wl_signal_add(&xdg_toplevel->base->surface->events.map, &view->map);
  1636. view->unmap.notify = xdg_toplevel_unmap;
  1637. wl_signal_add(&xdg_toplevel->base->surface->events.unmap, &view->unmap);
  1638. view->commit.notify = xdg_toplevel_commit;
  1639. wl_signal_add(&xdg_toplevel->base->surface->events.commit, &view->commit);
  1640. view->destroy.notify = xdg_toplevel_destroy;
  1641. wl_signal_add(&xdg_toplevel->events.destroy, &view->destroy);
  1642. view->request_move.notify = xdg_toplevel_request_move;
  1643. wl_signal_add(&xdg_toplevel->events.request_move, &view->request_move);
  1644. view->request_resize.notify = xdg_toplevel_request_resize;
  1645. wl_signal_add(&xdg_toplevel->events.request_resize, &view->request_resize);
  1646. view->request_maximize.notify = xdg_toplevel_request_maximize;
  1647. wl_signal_add(&xdg_toplevel->events.request_maximize, &view->request_maximize);
  1648. view->request_minimize.notify = xdg_toplevel_request_minimize;
  1649. wl_signal_add(&xdg_toplevel->events.request_minimize, &view->request_minimize);
  1650. view->request_fullscreen.notify = xdg_toplevel_request_fullscreen;
  1651. wl_signal_add(&xdg_toplevel->events.request_fullscreen, &view->request_fullscreen);
  1652. view->set_title.notify = xdg_toplevel_set_title;
  1653. wl_signal_add(&xdg_toplevel->events.set_title, &view->set_title);
  1654. view->set_app_id.notify = xdg_toplevel_set_app_id;
  1655. wl_signal_add(&xdg_toplevel->events.set_app_id, &view->set_app_id);
  1656. view_emit_new(view);
  1657. }
  1658. /* popups */
  1659. static void xdg_popup_commit(struct wl_listener *listener, void *data) {
  1660. (void) data;
  1661. struct mjc_popup *popup = wl_container_of(listener, popup, commit);
  1662. if (popup->xdg_popup->base->initial_commit) {
  1663. wlr_xdg_surface_schedule_configure(popup->xdg_popup->base);
  1664. }
  1665. }
  1666. static void xdg_popup_destroy(struct wl_listener *listener, void *data) {
  1667. (void) data;
  1668. struct mjc_popup *popup = wl_container_of(listener, popup, destroy);
  1669. wl_list_remove(&popup->commit.link);
  1670. wl_list_remove(&popup->destroy.link);
  1671. free(popup);
  1672. }
  1673. static void server_new_xdg_popup(struct wl_listener *listener, void *data) {
  1674. (void) listener;
  1675. struct wlr_xdg_popup *xdg_popup = data;
  1676. struct wlr_xdg_surface *parent =
  1677. wlr_xdg_surface_try_from_wlr_surface(xdg_popup->parent);
  1678. if (parent == NULL || parent->data == NULL) {
  1679. return;
  1680. }
  1681. struct wlr_scene_tree *parent_tree = parent->data;
  1682. xdg_popup->base->data =
  1683. wlr_scene_xdg_surface_create(parent_tree, xdg_popup->base);
  1684. struct mjc_popup *popup = calloc(1, sizeof(*popup));
  1685. popup->xdg_popup = xdg_popup;
  1686. popup->commit.notify = xdg_popup_commit;
  1687. wl_signal_add(&xdg_popup->base->surface->events.commit, &popup->commit);
  1688. popup->destroy.notify = xdg_popup_destroy;
  1689. wl_signal_add(&xdg_popup->events.destroy, &popup->destroy);
  1690. }
  1691. /* decorations - the compositor draws GNOME-style server-side frames. We grant
  1692. * SERVER_SIDE unless the client explicitly demands client-side decorations, in
  1693. * which case we honor it and leave that window chromeless (no server frame). */
  1694. static void decoration_request_mode(struct wl_listener *listener, void *data) {
  1695. (void) data;
  1696. struct mjc_decoration *deco = wl_container_of(listener, deco, request_mode);
  1697. if (!deco->decoration->toplevel->base->initialized) {
  1698. return;
  1699. }
  1700. /* Always grant server-side decorations so the client drops its own CSD and
  1701. * content aligns with our frame. Chromeless is only via Policy (shell) or IPC. */
  1702. wlr_xdg_toplevel_decoration_v1_set_mode(deco->decoration,
  1703. WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
  1704. if (deco->view != NULL) {
  1705. view_decoration_update(deco->view);
  1706. }
  1707. }
  1708. static void decoration_destroy(struct wl_listener *listener, void *data) {
  1709. (void) data;
  1710. struct mjc_decoration *deco = wl_container_of(listener, deco, destroy);
  1711. wl_list_remove(&deco->request_mode.link);
  1712. wl_list_remove(&deco->destroy.link);
  1713. free(deco);
  1714. }
  1715. static void server_new_toplevel_decoration(struct wl_listener *listener,
  1716. void *data) {
  1717. (void) listener;
  1718. struct wlr_xdg_toplevel_decoration_v1 *decoration = data;
  1719. struct mjc_decoration *deco = calloc(1, sizeof(*deco));
  1720. deco->decoration = decoration;
  1721. /* the view was stashed on the xdg surface in server_new_xdg_toplevel */
  1722. struct wlr_scene_tree *tree = decoration->toplevel->base->data;
  1723. deco->view = tree != NULL ? tree->node.data : NULL;
  1724. deco->request_mode.notify = decoration_request_mode;
  1725. wl_signal_add(&decoration->events.request_mode, &deco->request_mode);
  1726. deco->destroy.notify = decoration_destroy;
  1727. wl_signal_add(&decoration->events.destroy, &deco->destroy);
  1728. decoration_request_mode(&deco->request_mode, NULL);
  1729. }
  1730. /* ------------------------------------------------------------------ */
  1731. /* xwayland views */
  1732. /* ------------------------------------------------------------------ */
  1733. static void xwayland_surface_map(struct wl_listener *listener, void *data) {
  1734. (void) data;
  1735. struct mjc_view *view = wl_container_of(listener, view, map);
  1736. struct mjc_server *server = view->server;
  1737. struct wlr_xwayland_surface *xsurface = view->xsurface;
  1738. mjc_layer layer = xsurface->override_redirect
  1739. ? MJC_LAYER_OVERLAY : view->layer;
  1740. view->scene_tree = wlr_scene_tree_create(server->layers[layer]);
  1741. view->scene_tree->node.data = view;
  1742. wlr_scene_surface_create(view->scene_tree, xsurface->surface);
  1743. wlr_scene_node_set_position(&view->scene_tree->node,
  1744. xsurface->x, xsurface->y);
  1745. view->mapped = true;
  1746. view_emit_map(view);
  1747. view_decoration_update(view);
  1748. /* X clients that did not place themselves land at 0,0 - center those */
  1749. if (!xsurface->override_redirect && xsurface->x == 0 && xsurface->y == 0) {
  1750. view_center(view);
  1751. }
  1752. if (!xsurface->override_redirect) {
  1753. mjc_view_focus(view);
  1754. }
  1755. }
  1756. static void xwayland_surface_unmap(struct wl_listener *listener, void *data) {
  1757. (void) data;
  1758. struct mjc_view *view = wl_container_of(listener, view, unmap);
  1759. view->mapped = false;
  1760. clear_focus_if(view->server, view);
  1761. /* emit while the scene node still exists so the policy side can
  1762. * read the last window geometry */
  1763. view_emit_unmap(view);
  1764. /* tear down the frame before the scene_tree (its children) is destroyed */
  1765. view_decoration_destroy(view);
  1766. if (view->scene_tree != NULL) {
  1767. wlr_scene_node_destroy(&view->scene_tree->node);
  1768. view->scene_tree = NULL;
  1769. }
  1770. }
  1771. static void xwayland_surface_associate(struct wl_listener *listener, void *data) {
  1772. (void) data;
  1773. struct mjc_view *view = wl_container_of(listener, view, associate);
  1774. view->map.notify = xwayland_surface_map;
  1775. wl_signal_add(&view->xsurface->surface->events.map, &view->map);
  1776. view->unmap.notify = xwayland_surface_unmap;
  1777. wl_signal_add(&view->xsurface->surface->events.unmap, &view->unmap);
  1778. }
  1779. static void xwayland_surface_dissociate(struct wl_listener *listener, void *data) {
  1780. (void) data;
  1781. struct mjc_view *view = wl_container_of(listener, view, dissociate);
  1782. wl_list_remove(&view->map.link);
  1783. wl_list_remove(&view->unmap.link);
  1784. }
  1785. static void xwayland_surface_destroy(struct wl_listener *listener, void *data) {
  1786. (void) data;
  1787. struct mjc_view *view = wl_container_of(listener, view, destroy);
  1788. wl_list_remove(&view->associate.link);
  1789. wl_list_remove(&view->dissociate.link);
  1790. wl_list_remove(&view->destroy.link);
  1791. wl_list_remove(&view->request_configure.link);
  1792. wl_list_remove(&view->request_move.link);
  1793. wl_list_remove(&view->request_resize.link);
  1794. wl_list_remove(&view->request_minimize.link);
  1795. wl_list_remove(&view->request_maximize.link);
  1796. wl_list_remove(&view->set_title.link);
  1797. wl_list_remove(&view->set_app_id.link);
  1798. wl_list_remove(&view->set_geometry.link);
  1799. view_decoration_destroy(view);
  1800. if (view->scene_tree != NULL) {
  1801. wlr_scene_node_destroy(&view->scene_tree->node);
  1802. view->scene_tree = NULL;
  1803. }
  1804. view_free(view);
  1805. }
  1806. static void xwayland_surface_request_configure(struct wl_listener *listener,
  1807. void *data) {
  1808. struct mjc_view *view = wl_container_of(listener, view, request_configure);
  1809. struct wlr_xwayland_surface_configure_event *event = data;
  1810. wlr_xwayland_surface_configure(view->xsurface,
  1811. event->x, event->y, event->width, event->height);
  1812. if (view->scene_tree != NULL) {
  1813. wlr_scene_node_set_position(&view->scene_tree->node, event->x, event->y);
  1814. }
  1815. if (view->decorated) {
  1816. view_decoration_update(view);
  1817. }
  1818. }
  1819. static void xwayland_surface_set_geometry(struct wl_listener *listener,
  1820. void *data) {
  1821. (void) data;
  1822. struct mjc_view *view = wl_container_of(listener, view, set_geometry);
  1823. if (view->scene_tree != NULL && view->xsurface != NULL) {
  1824. wlr_scene_node_set_position(&view->scene_tree->node,
  1825. view->xsurface->x, view->xsurface->y);
  1826. }
  1827. if (view->decorated) {
  1828. view_decoration_update(view);
  1829. }
  1830. }
  1831. static void xwayland_surface_request_move(struct wl_listener *listener,
  1832. void *data) {
  1833. (void) data;
  1834. struct mjc_view *view = wl_container_of(listener, view, request_move);
  1835. begin_interactive(view, MJC_CURSOR_MOVE, 0);
  1836. }
  1837. static void xwayland_surface_request_resize(struct wl_listener *listener,
  1838. void *data) {
  1839. struct mjc_view *view = wl_container_of(listener, view, request_resize);
  1840. struct wlr_xwayland_resize_event *event = data;
  1841. begin_interactive(view, MJC_CURSOR_RESIZE, event->edges);
  1842. }
  1843. static void xwayland_surface_request_minimize(struct wl_listener *listener,
  1844. void *data) {
  1845. (void) data;
  1846. struct mjc_view *view = wl_container_of(listener, view, request_minimize);
  1847. mjc_view_set_minimized(view, true);
  1848. }
  1849. static void xwayland_surface_request_maximize(struct wl_listener *listener,
  1850. void *data) {
  1851. (void) data;
  1852. struct mjc_view *view = wl_container_of(listener, view, request_maximize);
  1853. struct wlr_xwayland_surface *xsurface = view->xsurface;
  1854. if (xsurface == NULL) {
  1855. return;
  1856. }
  1857. /* the client announces the state it wants via _NET_WM_STATE */
  1858. mjc_view_set_maximized(view,
  1859. xsurface->maximized_vert && xsurface->maximized_horz);
  1860. }
  1861. static void xwayland_surface_set_title(struct wl_listener *listener, void *data) {
  1862. (void) data;
  1863. struct mjc_view *view = wl_container_of(listener, view, set_title);
  1864. deco_repaint(view);
  1865. if (view->server->cbs.view_title != NULL) {
  1866. view->server->cbs.view_title(view->server->ud, view,
  1867. view->xsurface->title);
  1868. }
  1869. }
  1870. static void xwayland_surface_set_class(struct wl_listener *listener, void *data) {
  1871. (void) data;
  1872. struct mjc_view *view = wl_container_of(listener, view, set_app_id);
  1873. if (view->server->cbs.view_app_id != NULL) {
  1874. view->server->cbs.view_app_id(view->server->ud, view,
  1875. view->xsurface->class);
  1876. }
  1877. }
  1878. static void server_new_xwayland_surface(struct wl_listener *listener, void *data) {
  1879. struct mjc_server *server =
  1880. wl_container_of(listener, server, new_xwayland_surface);
  1881. struct wlr_xwayland_surface *xsurface = data;
  1882. struct mjc_view *view = view_create(server, true);
  1883. view->xsurface = xsurface;
  1884. if (xsurface->override_redirect) {
  1885. view->focusable = false;
  1886. }
  1887. xsurface->data = view;
  1888. view->associate.notify = xwayland_surface_associate;
  1889. wl_signal_add(&xsurface->events.associate, &view->associate);
  1890. view->dissociate.notify = xwayland_surface_dissociate;
  1891. wl_signal_add(&xsurface->events.dissociate, &view->dissociate);
  1892. view->destroy.notify = xwayland_surface_destroy;
  1893. wl_signal_add(&xsurface->events.destroy, &view->destroy);
  1894. view->request_configure.notify = xwayland_surface_request_configure;
  1895. wl_signal_add(&xsurface->events.request_configure, &view->request_configure);
  1896. view->request_move.notify = xwayland_surface_request_move;
  1897. wl_signal_add(&xsurface->events.request_move, &view->request_move);
  1898. view->request_resize.notify = xwayland_surface_request_resize;
  1899. wl_signal_add(&xsurface->events.request_resize, &view->request_resize);
  1900. view->request_minimize.notify = xwayland_surface_request_minimize;
  1901. wl_signal_add(&xsurface->events.request_minimize, &view->request_minimize);
  1902. view->request_maximize.notify = xwayland_surface_request_maximize;
  1903. wl_signal_add(&xsurface->events.request_maximize, &view->request_maximize);
  1904. view->set_title.notify = xwayland_surface_set_title;
  1905. wl_signal_add(&xsurface->events.set_title, &view->set_title);
  1906. view->set_app_id.notify = xwayland_surface_set_class;
  1907. wl_signal_add(&xsurface->events.set_class, &view->set_app_id);
  1908. view->set_geometry.notify = xwayland_surface_set_geometry;
  1909. wl_signal_add(&xsurface->events.set_geometry, &view->set_geometry);
  1910. view_emit_new(view);
  1911. }
  1912. static void server_xwayland_ready(struct wl_listener *listener, void *data) {
  1913. (void) data;
  1914. struct mjc_server *server = wl_container_of(listener, server, xwayland_ready);
  1915. wlr_xwayland_set_seat(server->xwayland, server->seat);
  1916. }
  1917. /* ------------------------------------------------------------------ */
  1918. /* sigchld */
  1919. /* ------------------------------------------------------------------ */
  1920. static int handle_sigchld(int signal_number, void *data) {
  1921. (void) signal_number;
  1922. struct mjc_server *server = data;
  1923. for (int i = 0; i < server->spawned_count;) {
  1924. int status;
  1925. pid_t pid = waitpid(server->spawned_pids[i], &status, WNOHANG);
  1926. if (pid > 0) {
  1927. server->spawned_pids[i] =
  1928. server->spawned_pids[--server->spawned_count];
  1929. if (server->cbs.child_exit != NULL) {
  1930. server->cbs.child_exit(server->ud, (int) pid, status);
  1931. }
  1932. } else {
  1933. i++;
  1934. }
  1935. }
  1936. return 0;
  1937. }
  1938. /* True when the GLES2 renderer ended up on software rendering (llvmpipe/swrast) — i.e. there is
  1939. * no usable hardware GL. In that case the Compose shell (Skiko) cannot create an OpenGL/GLX
  1940. * context on the (also software) XWayland and must use its CPU raster backend, so the compositor
  1941. * exports SKIKO_RENDER_API=SOFTWARE for the shell. On a real GPU it returns false and the shell
  1942. * uses hardware GL. Queries GL_RENDERER via the renderer's own EGL context (surfaceless). */
  1943. static bool mjc_renderer_is_software(struct wlr_renderer *renderer) {
  1944. if (!wlr_renderer_is_gles2(renderer)) {
  1945. return false;
  1946. }
  1947. struct wlr_egl *egl = wlr_gles2_renderer_get_egl(renderer);
  1948. if (egl == NULL) {
  1949. return false;
  1950. }
  1951. EGLDisplay dpy = wlr_egl_get_display(egl);
  1952. EGLContext ctx = wlr_egl_get_context(egl);
  1953. if (dpy == EGL_NO_DISPLAY || ctx == EGL_NO_CONTEXT) {
  1954. return false;
  1955. }
  1956. if (!eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx)) {
  1957. return false;
  1958. }
  1959. const char *gl_renderer = (const char *) glGetString(GL_RENDERER);
  1960. bool software = gl_renderer != NULL && (
  1961. strstr(gl_renderer, "llvmpipe") != NULL ||
  1962. strstr(gl_renderer, "softpipe") != NULL ||
  1963. strstr(gl_renderer, "swrast") != NULL ||
  1964. strstr(gl_renderer, "SWR") != NULL ||
  1965. strstr(gl_renderer, "Software") != NULL);
  1966. eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  1967. return software;
  1968. }
  1969. /* ------------------------------------------------------------------ */
  1970. /* public API */
  1971. /* ------------------------------------------------------------------ */
  1972. mjc_server *mjc_create(void) {
  1973. struct mjc_server *server = calloc(1, sizeof(*server));
  1974. wl_list_init(&server->views);
  1975. wl_list_init(&server->keyboards);
  1976. wl_list_init(&server->fd_sources);
  1977. return server;
  1978. }
  1979. bool mjc_start(mjc_server *server, const mjc_callbacks *callbacks, void *userdata) {
  1980. server->cbs = *callbacks;
  1981. server->ud = userdata;
  1982. /* default decoration theme (dark slate + near-white); the shell overrides this
  1983. * from its live background palette via mjc_set_decoration_theme over IPC */
  1984. server->deco_bg[0] = 0.16f;
  1985. server->deco_bg[1] = 0.17f;
  1986. server->deco_bg[2] = 0.20f;
  1987. server->deco_fg[0] = 0.92f;
  1988. server->deco_fg[1] = 0.93f;
  1989. server->deco_fg[2] = 0.95f;
  1990. server->deco_icon_bg[0] = 0.28f;
  1991. server->deco_icon_bg[1] = 0.29f;
  1992. server->deco_icon_bg[2] = 0.32f;
  1993. server->deco_icon_fg[0] = 0.55f;
  1994. server->deco_icon_fg[1] = 0.56f;
  1995. server->deco_icon_fg[2] = 0.58f;
  1996. wlr_log_init(WLR_INFO, NULL);
  1997. /* nested-output size, 16:9 default; override with MJDEVC_OUTPUT=WIDTHxHEIGHT */
  1998. server->output_w = 1280;
  1999. server->output_h = 720;
  2000. const char *out_env = getenv("MJDEVC_OUTPUT");
  2001. if (out_env != NULL) {
  2002. int w = 0, h = 0;
  2003. if (sscanf(out_env, "%dx%d", &w, &h) == 2 && w > 0 && h > 0) {
  2004. server->output_w = w;
  2005. server->output_h = h;
  2006. }
  2007. }
  2008. server->display = wl_display_create();
  2009. server->loop = wl_display_get_event_loop(server->display);
  2010. server->backend = wlr_backend_autocreate(server->loop, &server->session);
  2011. if (server->backend == NULL) {
  2012. fprintf(stderr, "mjdevc: failed to create wlr_backend\n");
  2013. return false;
  2014. }
  2015. server->renderer = wlr_renderer_autocreate(server->backend);
  2016. if (server->renderer == NULL) {
  2017. fprintf(stderr, "mjdevc: failed to create wlr_renderer\n");
  2018. return false;
  2019. }
  2020. wlr_renderer_init_wl_display(server->renderer, server->display);
  2021. server->allocator = wlr_allocator_autocreate(server->backend, server->renderer);
  2022. if (server->allocator == NULL) {
  2023. fprintf(stderr, "mjdevc: failed to create wlr_allocator\n");
  2024. return false;
  2025. }
  2026. server->compositor = wlr_compositor_create(server->display, 5, server->renderer);
  2027. wlr_subcompositor_create(server->display);
  2028. wlr_data_device_manager_create(server->display);
  2029. server->output_layout = wlr_output_layout_create(server->display);
  2030. server->new_output.notify = server_new_output;
  2031. wl_signal_add(&server->backend->events.new_output, &server->new_output);
  2032. server->scene = wlr_scene_create();
  2033. server->scene_layout =
  2034. wlr_scene_attach_output_layout(server->scene, server->output_layout);
  2035. for (int i = 0; i < 5; i++) {
  2036. server->layers[i] = wlr_scene_tree_create(&server->scene->tree);
  2037. }
  2038. /* injection feedback marker: a 16x16 white square in the overlay layer, hidden until
  2039. * input is injected, then shown at the pointer and faded out (Android show-touches style) */
  2040. const float marker_hidden[4] = {0.0f, 0.0f, 0.0f, 0.0f};
  2041. server->inject_marker = wlr_scene_rect_create(
  2042. server->layers[MJC_LAYER_OVERLAY], 16, 16, marker_hidden);
  2043. wlr_scene_node_set_enabled(&server->inject_marker->node, false);
  2044. server->xdg_shell = wlr_xdg_shell_create(server->display, 3);
  2045. server->new_xdg_toplevel.notify = server_new_xdg_toplevel;
  2046. wl_signal_add(&server->xdg_shell->events.new_toplevel, &server->new_xdg_toplevel);
  2047. server->new_xdg_popup.notify = server_new_xdg_popup;
  2048. wl_signal_add(&server->xdg_shell->events.new_popup, &server->new_xdg_popup);
  2049. server->xdg_decoration_mgr = wlr_xdg_decoration_manager_v1_create(server->display);
  2050. server->new_toplevel_decoration.notify = server_new_toplevel_decoration;
  2051. wl_signal_add(&server->xdg_decoration_mgr->events.new_toplevel_decoration,
  2052. &server->new_toplevel_decoration);
  2053. server->cursor = wlr_cursor_create();
  2054. wlr_cursor_attach_output_layout(server->cursor, server->output_layout);
  2055. server->cursor_mgr = wlr_xcursor_manager_create(NULL, 24);
  2056. server->cursor_mode = MJC_CURSOR_PASSTHROUGH;
  2057. server->cursor_motion.notify = server_cursor_motion;
  2058. wl_signal_add(&server->cursor->events.motion, &server->cursor_motion);
  2059. server->cursor_motion_absolute.notify = server_cursor_motion_absolute;
  2060. wl_signal_add(&server->cursor->events.motion_absolute,
  2061. &server->cursor_motion_absolute);
  2062. server->cursor_button.notify = server_cursor_button;
  2063. wl_signal_add(&server->cursor->events.button, &server->cursor_button);
  2064. server->cursor_axis.notify = server_cursor_axis;
  2065. wl_signal_add(&server->cursor->events.axis, &server->cursor_axis);
  2066. server->cursor_frame.notify = server_cursor_frame;
  2067. wl_signal_add(&server->cursor->events.frame, &server->cursor_frame);
  2068. server->new_input.notify = server_new_input;
  2069. wl_signal_add(&server->backend->events.new_input, &server->new_input);
  2070. server->seat = wlr_seat_create(server->display, "seat0");
  2071. server->request_cursor.notify = seat_request_cursor;
  2072. wl_signal_add(&server->seat->events.request_set_cursor, &server->request_cursor);
  2073. server->request_set_selection.notify = seat_request_set_selection;
  2074. wl_signal_add(&server->seat->events.request_set_selection,
  2075. &server->request_set_selection);
  2076. const char *socket = wl_display_add_socket_auto(server->display);
  2077. if (socket == NULL) {
  2078. fprintf(stderr, "mjdevc: failed to create wayland socket\n");
  2079. return false;
  2080. }
  2081. server->socket = socket;
  2082. server->xwayland = wlr_xwayland_create(server->display, server->compositor, true);
  2083. if (server->xwayland != NULL) {
  2084. server->new_xwayland_surface.notify = server_new_xwayland_surface;
  2085. wl_signal_add(&server->xwayland->events.new_surface,
  2086. &server->new_xwayland_surface);
  2087. server->xwayland_ready.notify = server_xwayland_ready;
  2088. wl_signal_add(&server->xwayland->events.ready, &server->xwayland_ready);
  2089. }
  2090. server->sigchld_source = wl_event_loop_add_signal(server->loop,
  2091. SIGCHLD, handle_sigchld, server);
  2092. if (!wlr_backend_start(server->backend)) {
  2093. fprintf(stderr, "mjdevc: failed to start backend\n");
  2094. return false;
  2095. }
  2096. setenv("WAYLAND_DISPLAY", socket, true);
  2097. if (server->xwayland != NULL) {
  2098. setenv("DISPLAY", server->xwayland->display_name, true);
  2099. }
  2100. /* GPU vs software decision for the Compose shell (Skiko), exported into its environment
  2101. * before it is spawned. Software GL (VM / no GPU) -> Skiko CPU raster (else it crashes with
  2102. * "Cannot create Linux GL context"); real GPU -> leave unset so Skiko uses hardware GL. */
  2103. if (mjc_renderer_is_software(server->renderer)) {
  2104. setenv("SKIKO_RENDER_API", "SOFTWARE", true);
  2105. fprintf(stderr, "mjdevc: software GL renderer -> SKIKO_RENDER_API=SOFTWARE for the shell\n");
  2106. } else {
  2107. unsetenv("SKIKO_RENDER_API");
  2108. fprintf(stderr, "mjdevc: hardware GL renderer -> shell uses Skiko OpenGL\n");
  2109. }
  2110. if (server->cbs.ready != NULL) {
  2111. server->cbs.ready(server->ud);
  2112. }
  2113. return true;
  2114. }
  2115. void mjc_run(mjc_server *server) {
  2116. wl_display_run(server->display);
  2117. }
  2118. void mjc_terminate(mjc_server *server) {
  2119. wl_display_terminate(server->display);
  2120. }
  2121. void mjc_destroy(mjc_server *server) {
  2122. if (server->xwayland != NULL) {
  2123. wlr_xwayland_destroy(server->xwayland);
  2124. }
  2125. wl_display_destroy_clients(server->display);
  2126. wlr_scene_node_destroy(&server->scene->tree.node);
  2127. wlr_xcursor_manager_destroy(server->cursor_mgr);
  2128. wlr_cursor_destroy(server->cursor);
  2129. wlr_allocator_destroy(server->allocator);
  2130. wlr_renderer_destroy(server->renderer);
  2131. wlr_backend_destroy(server->backend);
  2132. wl_display_destroy(server->display);
  2133. free(server);
  2134. }
  2135. const char *mjc_socket_name(mjc_server *server) {
  2136. return server->socket;
  2137. }
  2138. const char *mjc_xwayland_display(mjc_server *server) {
  2139. return server->xwayland != NULL ? server->xwayland->display_name : NULL;
  2140. }
  2141. void mjc_output_size(mjc_server *server, int *width, int *height) {
  2142. struct wlr_box box;
  2143. wlr_output_layout_get_box(server->output_layout, NULL, &box);
  2144. *width = box.width;
  2145. *height = box.height;
  2146. }
  2147. int mjc_spawn(mjc_server *server, const char *command) {
  2148. pid_t pid = fork();
  2149. if (pid < 0) {
  2150. return -1;
  2151. }
  2152. if (pid == 0) {
  2153. setsid();
  2154. sigset_t set;
  2155. sigemptyset(&set);
  2156. sigprocmask(SIG_SETMASK, &set, NULL);
  2157. execl("/bin/sh", "/bin/sh", "-c", command, (void *) NULL);
  2158. _exit(127);
  2159. }
  2160. if (server->spawned_count <
  2161. (int) (sizeof(server->spawned_pids) / sizeof(server->spawned_pids[0]))) {
  2162. server->spawned_pids[server->spawned_count++] = pid;
  2163. }
  2164. return (int) pid;
  2165. }
  2166. /* unix socket helpers */
  2167. #include <fcntl.h>
  2168. #include <sys/socket.h>
  2169. #include <sys/un.h>
  2170. int mjc_unix_listen(const char *path, int backlog) {
  2171. int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  2172. if (fd < 0) {
  2173. return -1;
  2174. }
  2175. struct sockaddr_un addr = {0};
  2176. addr.sun_family = AF_UNIX;
  2177. if (strlen(path) >= sizeof(addr.sun_path)) {
  2178. close(fd);
  2179. return -1;
  2180. }
  2181. strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
  2182. unlink(path);
  2183. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) != 0 ||
  2184. listen(fd, backlog) != 0) {
  2185. close(fd);
  2186. return -1;
  2187. }
  2188. return fd;
  2189. }
  2190. int mjc_unix_accept(int server_fd) {
  2191. int fd = accept(server_fd, NULL, NULL);
  2192. if (fd >= 0) {
  2193. fcntl(fd, F_SETFL, O_NONBLOCK);
  2194. }
  2195. return fd;
  2196. }
  2197. /* fd integration */
  2198. static int handle_fd_event(int fd, uint32_t mask, void *data) {
  2199. struct mjc_fd_source *source = data;
  2200. if (source->server->cbs.fd_event != NULL) {
  2201. return source->server->cbs.fd_event(source->server->ud, fd, mask);
  2202. }
  2203. return 0;
  2204. }
  2205. bool mjc_loop_add_fd(mjc_server *server, int fd, uint32_t mask) {
  2206. struct mjc_fd_source *source = calloc(1, sizeof(*source));
  2207. source->server = server;
  2208. source->fd = fd;
  2209. source->source = wl_event_loop_add_fd(server->loop, fd, mask,
  2210. handle_fd_event, source);
  2211. if (source->source == NULL) {
  2212. free(source);
  2213. return false;
  2214. }
  2215. wl_list_insert(&server->fd_sources, &source->link);
  2216. return true;
  2217. }
  2218. void mjc_loop_remove_fd(mjc_server *server, int fd) {
  2219. struct mjc_fd_source *source, *tmp;
  2220. wl_list_for_each_safe(source, tmp, &server->fd_sources, link) {
  2221. if (source->fd == fd) {
  2222. wl_event_source_remove(source->source);
  2223. wl_list_remove(&source->link);
  2224. free(source);
  2225. return;
  2226. }
  2227. }
  2228. }
  2229. /* views */
  2230. uint64_t mjc_view_id(mjc_view *view) {
  2231. return view->id;
  2232. }
  2233. int mjc_view_pid(mjc_view *view) {
  2234. if (view->is_xwayland) {
  2235. return view->xsurface != NULL ? (int) view->xsurface->pid : 0;
  2236. }
  2237. pid_t pid = 0;
  2238. struct wlr_surface *surface = view_surface(view);
  2239. if (surface != NULL) {
  2240. struct wl_client *client = wl_resource_get_client(surface->resource);
  2241. wl_client_get_credentials(client, &pid, NULL, NULL);
  2242. }
  2243. return (int) pid;
  2244. }
  2245. const char *mjc_view_title(mjc_view *view) {
  2246. if (view->is_xwayland) {
  2247. return view->xsurface != NULL ? view->xsurface->title : NULL;
  2248. }
  2249. return view->xdg_toplevel->title;
  2250. }
  2251. const char *mjc_view_app_id(mjc_view *view) {
  2252. if (view->is_xwayland) {
  2253. return view->xsurface != NULL ? view->xsurface->class : NULL;
  2254. }
  2255. return view->xdg_toplevel->app_id;
  2256. }
  2257. bool mjc_view_is_xwayland(mjc_view *view) {
  2258. return view->is_xwayland;
  2259. }
  2260. bool mjc_view_is_mapped(mjc_view *view) {
  2261. return view->mapped;
  2262. }
  2263. bool mjc_view_is_minimized(mjc_view *view) {
  2264. return view->minimized;
  2265. }
  2266. bool mjc_view_is_maximized(mjc_view *view) {
  2267. return view->maximized;
  2268. }
  2269. bool mjc_view_is_focused(mjc_view *view) {
  2270. return view->server->focused_view == view;
  2271. }
  2272. void mjc_view_get_geometry(mjc_view *view, int *x, int *y, int *width, int *height) {
  2273. struct wlr_box box = {0};
  2274. if (view->scene_tree != NULL || !view->is_xwayland) {
  2275. view_visible_geometry(view, &box);
  2276. }
  2277. *x = box.x;
  2278. *y = box.y;
  2279. *width = box.width;
  2280. *height = box.height;
  2281. }
  2282. void mjc_view_close(mjc_view *view) {
  2283. if (view->is_xwayland) {
  2284. if (view->xsurface != NULL) {
  2285. wlr_xwayland_surface_close(view->xsurface);
  2286. }
  2287. } else {
  2288. wlr_xdg_toplevel_send_close(view->xdg_toplevel);
  2289. }
  2290. }
  2291. void mjc_view_set_minimized(mjc_view *view, bool minimized) {
  2292. if (view->minimized == minimized) {
  2293. return;
  2294. }
  2295. view->minimized = minimized;
  2296. if (view->scene_tree != NULL) {
  2297. wlr_scene_node_set_enabled(&view->scene_tree->node, !minimized);
  2298. }
  2299. if (view->is_xwayland && view->xsurface != NULL) {
  2300. wlr_xwayland_surface_set_minimized(view->xsurface, minimized);
  2301. }
  2302. if (minimized) {
  2303. clear_focus_if(view->server, view);
  2304. } else {
  2305. mjc_view_focus(view);
  2306. }
  2307. }
  2308. void mjc_view_set_maximized(mjc_view *view, bool maximized) {
  2309. struct mjc_server *server = view->server;
  2310. if (view->maximized == maximized) {
  2311. return;
  2312. }
  2313. struct wlr_box box;
  2314. wlr_output_layout_get_box(server->output_layout, NULL, &box);
  2315. if (maximized && view->mapped && view->scene_tree != NULL) {
  2316. /* remember the outer frame geometry for restore */
  2317. view_visible_geometry(view, &view->saved_geo);
  2318. }
  2319. view->maximized = maximized;
  2320. int top, left, right, bottom;
  2321. view_deco_insets(view, &top, &left, &right, &bottom);
  2322. int cw = box.width - left - right;
  2323. int ch = box.height - top - bottom;
  2324. if (cw < 1) {
  2325. cw = 1;
  2326. }
  2327. if (ch < 1) {
  2328. ch = 1;
  2329. }
  2330. if (view->is_xwayland) {
  2331. if (view->xsurface == NULL) {
  2332. return;
  2333. }
  2334. wlr_xwayland_surface_set_maximized(view->xsurface, maximized);
  2335. if (maximized) {
  2336. wlr_xwayland_surface_configure(view->xsurface,
  2337. (int16_t)(box.x + left), (int16_t)(box.y + top),
  2338. (uint16_t) cw, (uint16_t) ch);
  2339. view_place_visible_topleft(view, box.x, box.y);
  2340. } else if (view->saved_geo.width > 0) {
  2341. int rw = view->saved_geo.width - left - right;
  2342. int rh = view->saved_geo.height - top - bottom;
  2343. if (rw < 1) {
  2344. rw = 1;
  2345. }
  2346. if (rh < 1) {
  2347. rh = 1;
  2348. }
  2349. wlr_xwayland_surface_configure(view->xsurface,
  2350. (int16_t)(view->saved_geo.x + left),
  2351. (int16_t)(view->saved_geo.y + top),
  2352. (uint16_t) rw, (uint16_t) rh);
  2353. view_place_visible_topleft(view, view->saved_geo.x, view->saved_geo.y);
  2354. }
  2355. } else {
  2356. wlr_xdg_toplevel_set_maximized(view->xdg_toplevel, maximized);
  2357. if (maximized) {
  2358. wlr_xdg_toplevel_set_size(view->xdg_toplevel, cw, ch);
  2359. view_place_visible_topleft(view, box.x, box.y);
  2360. } else {
  2361. int rw = view->saved_geo.width - left - right;
  2362. int rh = view->saved_geo.height - top - bottom;
  2363. if (rw < 1) {
  2364. rw = 1;
  2365. }
  2366. if (rh < 1) {
  2367. rh = 1;
  2368. }
  2369. wlr_xdg_toplevel_set_size(view->xdg_toplevel, rw, rh);
  2370. view->pending_center = true;
  2371. if (view->saved_geo.width > 0) {
  2372. view_place_visible_topleft(view, view->saved_geo.x, view->saved_geo.y);
  2373. }
  2374. }
  2375. }
  2376. /* xwayland sizes itself synchronously here (no commit round-trip), so refresh
  2377. * the frame now; xdg follows up on its next commit */
  2378. if (view->decorated) {
  2379. view_decoration_update(view);
  2380. }
  2381. }
  2382. void mjc_view_set_layer(mjc_view *view, mjc_layer layer) {
  2383. view->layer = layer;
  2384. if (view->scene_tree != NULL) {
  2385. wlr_scene_node_reparent(&view->scene_tree->node,
  2386. view->server->layers[layer]);
  2387. }
  2388. /* only normal-layer app windows get a server-side frame; shell surfaces
  2389. * (wallpaper, panels, menus) live in other layers and stay undecorated */
  2390. view_decoration_update(view);
  2391. }
  2392. void mjc_view_set_focusable(mjc_view *view, bool focusable) {
  2393. view->focusable = focusable;
  2394. if (!focusable) {
  2395. clear_focus_if(view->server, view);
  2396. }
  2397. }
  2398. void mjc_view_set_chrome(mjc_view *view, bool chrome) {
  2399. view->is_chrome = chrome;
  2400. view_decoration_update(view);
  2401. }
  2402. void mjc_view_set_decorated(mjc_view *view, bool decorated) {
  2403. view->deco_disabled = !decorated;
  2404. view_decoration_update(view);
  2405. }
  2406. void mjc_set_decoration_theme(mjc_server *server,
  2407. float bg_r, float bg_g, float bg_b,
  2408. float fg_r, float fg_g, float fg_b,
  2409. float icon_bg_r, float icon_bg_g, float icon_bg_b,
  2410. float icon_fg_r, float icon_fg_g, float icon_fg_b) {
  2411. server->deco_bg[0] = bg_r;
  2412. server->deco_bg[1] = bg_g;
  2413. server->deco_bg[2] = bg_b;
  2414. server->deco_fg[0] = fg_r;
  2415. server->deco_fg[1] = fg_g;
  2416. server->deco_fg[2] = fg_b;
  2417. server->deco_icon_bg[0] = icon_bg_r;
  2418. server->deco_icon_bg[1] = icon_bg_g;
  2419. server->deco_icon_bg[2] = icon_bg_b;
  2420. server->deco_icon_fg[0] = icon_fg_r;
  2421. server->deco_icon_fg[1] = icon_fg_g;
  2422. server->deco_icon_fg[2] = icon_fg_b;
  2423. /* re-skin every currently decorated window with the new palette */
  2424. struct mjc_view *view;
  2425. wl_list_for_each(view, &server->views, link) {
  2426. if (view->decorated) {
  2427. deco_repaint(view);
  2428. float border_col[4];
  2429. deco_border_color_rgba(server, border_col);
  2430. for (int i = 0; i < 3; i++) {
  2431. if (view->deco_border[i] != NULL) {
  2432. wlr_scene_rect_set_color(view->deco_border[i], border_col);
  2433. }
  2434. }
  2435. }
  2436. }
  2437. }
  2438. void mjc_view_set_position(mjc_view *view, int x, int y) {
  2439. if (view->scene_tree != NULL) {
  2440. wlr_scene_node_set_position(&view->scene_tree->node, x, y);
  2441. }
  2442. if (view->is_xwayland && view->xsurface != NULL) {
  2443. wlr_xwayland_surface_configure(view->xsurface,
  2444. (int16_t) x, (int16_t) y,
  2445. (uint16_t) view->xsurface->width, (uint16_t) view->xsurface->height);
  2446. }
  2447. }
  2448. void mjc_view_set_geometry(mjc_view *view, int x, int y, int width, int height) {
  2449. if (width <= 0 || height <= 0) {
  2450. return;
  2451. }
  2452. if (view->maximized) {
  2453. /* keep it as the restore target for the next unmaximize */
  2454. view->saved_geo.x = x;
  2455. view->saved_geo.y = y;
  2456. view->saved_geo.width = width;
  2457. view->saved_geo.height = height;
  2458. return;
  2459. }
  2460. int top, left, right, bottom;
  2461. view_deco_insets(view, &top, &left, &right, &bottom);
  2462. int cw = width - left - right;
  2463. int ch = height - top - bottom;
  2464. if (cw < 1) {
  2465. cw = 1;
  2466. }
  2467. if (ch < 1) {
  2468. ch = 1;
  2469. }
  2470. int cx = x + left;
  2471. int cy = y + top;
  2472. view_clamp_frame_pos(view->server, &x, &y, width, height);
  2473. cx = x + left;
  2474. cy = y + top;
  2475. if (view_should_decorate(view)) {
  2476. if (view->is_xwayland) {
  2477. if (view->xsurface != NULL) {
  2478. wlr_xwayland_surface_configure(view->xsurface,
  2479. (int16_t) cx, (int16_t) cy,
  2480. (uint16_t) cw, (uint16_t) ch);
  2481. }
  2482. } else {
  2483. wlr_xdg_toplevel_set_size(view->xdg_toplevel, cw, ch);
  2484. }
  2485. view_place_visible_topleft(view, x, y);
  2486. view->pending_center = false;
  2487. view->placement_set = true;
  2488. return;
  2489. }
  2490. if (view->is_xwayland) {
  2491. if (view->xsurface != NULL) {
  2492. wlr_xwayland_surface_configure(view->xsurface,
  2493. (int16_t) x, (int16_t) y, (uint16_t) width, (uint16_t) height);
  2494. }
  2495. if (view->scene_tree != NULL) {
  2496. wlr_scene_node_set_position(&view->scene_tree->node, x, y);
  2497. }
  2498. } else {
  2499. wlr_xdg_toplevel_set_size(view->xdg_toplevel, width, height);
  2500. if (view->scene_tree != NULL) {
  2501. struct wlr_box off;
  2502. wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &off);
  2503. wlr_scene_node_set_position(&view->scene_tree->node,
  2504. x - off.x, y - off.y);
  2505. }
  2506. }
  2507. view->pending_center = false;
  2508. view->placement_set = true;
  2509. if (view->decorated) {
  2510. view_decoration_update(view);
  2511. }
  2512. }
  2513. /* ------------------------------------------------------------------ */
  2514. /* input injection (ipc-driven, for automated/headless testing) */
  2515. /* ------------------------------------------------------------------ */
  2516. static uint32_t mjc_now_msec(void) {
  2517. struct timespec now;
  2518. clock_gettime(CLOCK_MONOTONIC, &now);
  2519. return (uint32_t) (now.tv_sec * 1000 + now.tv_nsec / 1000000);
  2520. }
  2521. #define MJC_INJECT_MARKER_SIZE 16
  2522. #define MJC_INJECT_FADE_STEP 0.08f
  2523. #define MJC_INJECT_FADE_INTERVAL_MS 40
  2524. /* step the injection marker's alpha down; disables it once fully faded */
  2525. static int inject_marker_fade(void *data) {
  2526. struct mjc_server *server = data;
  2527. if (server->inject_marker == NULL) {
  2528. return 0;
  2529. }
  2530. server->inject_alpha -= MJC_INJECT_FADE_STEP;
  2531. if (server->inject_alpha <= 0.0f) {
  2532. server->inject_alpha = 0.0f;
  2533. wlr_scene_node_set_enabled(&server->inject_marker->node, false);
  2534. return 0;
  2535. }
  2536. float a = server->inject_alpha;
  2537. const float color[4] = {a, a, a, a}; /* premultiplied white */
  2538. wlr_scene_rect_set_color(server->inject_marker, color);
  2539. if (server->inject_fade_timer != NULL) {
  2540. wl_event_source_timer_update(server->inject_fade_timer,
  2541. MJC_INJECT_FADE_INTERVAL_MS);
  2542. }
  2543. return 0;
  2544. }
  2545. /* show the injection marker at the current cursor position at full opacity and (re)arm
  2546. * the fade timer; called on every injected pointer event so it stays bright while moving */
  2547. static void inject_marker_show(struct mjc_server *server) {
  2548. if (server->inject_marker == NULL) {
  2549. return;
  2550. }
  2551. wlr_scene_node_set_position(&server->inject_marker->node,
  2552. (int) server->cursor->x - MJC_INJECT_MARKER_SIZE / 2,
  2553. (int) server->cursor->y - MJC_INJECT_MARKER_SIZE / 2);
  2554. server->inject_alpha = 1.0f;
  2555. const float color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
  2556. wlr_scene_rect_set_color(server->inject_marker, color);
  2557. wlr_scene_node_raise_to_top(&server->inject_marker->node);
  2558. wlr_scene_node_set_enabled(&server->inject_marker->node, true);
  2559. /* keep the marker persistently visible for now (behaves like a mouse cursor); the
  2560. * inactivity fadeout is disabled until we want it back */
  2561. }
  2562. void mjc_pointer_move(mjc_server *server, int x, int y) {
  2563. if (server == NULL) {
  2564. return;
  2565. }
  2566. /* warp to the closest valid layout point, then run the normal motion path so
  2567. * pointer focus, focus-follows-mouse and the ipc pointer broadcast all fire */
  2568. wlr_cursor_warp_closest(server->cursor, NULL, (double) x, (double) y);
  2569. process_cursor_motion(server, mjc_now_msec());
  2570. inject_marker_show(server);
  2571. }
  2572. void mjc_pointer_button(mjc_server *server, uint32_t button, bool pressed) {
  2573. if (server == NULL) {
  2574. return;
  2575. }
  2576. enum wl_pointer_button_state state = pressed
  2577. ? WL_POINTER_BUTTON_STATE_PRESSED
  2578. : WL_POINTER_BUTTON_STATE_RELEASED;
  2579. wlr_seat_pointer_notify_button(server->seat, mjc_now_msec(), button, state);
  2580. if (!pressed) {
  2581. server->cursor_mode = MJC_CURSOR_PASSTHROUGH;
  2582. server->grabbed_view = NULL;
  2583. } else if (deco_handle_button(server, server->cursor->x, server->cursor->y, true)) {
  2584. /* titlebar buttons / resize edges — same path as real pointer input */
  2585. } else {
  2586. double sx, sy;
  2587. struct wlr_surface *surface = NULL;
  2588. struct mjc_view *view = desktop_view_at(server,
  2589. server->cursor->x, server->cursor->y, &surface, &sx, &sy);
  2590. if (view != NULL && view->focusable) {
  2591. mjc_view_focus(view);
  2592. }
  2593. }
  2594. wlr_seat_pointer_notify_frame(server->seat);
  2595. inject_marker_show(server);
  2596. }
  2597. void mjc_key(mjc_server *server, uint32_t keycode, bool pressed) {
  2598. if (server == NULL) {
  2599. return;
  2600. }
  2601. struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(server->seat);
  2602. if (keyboard == NULL) {
  2603. if (wl_list_empty(&server->keyboards)) {
  2604. return;
  2605. }
  2606. struct mjc_keyboard *kb =
  2607. wl_container_of(server->keyboards.next, kb, link);
  2608. keyboard = kb->wlr_keyboard;
  2609. }
  2610. enum wl_keyboard_key_state state = pressed
  2611. ? WL_KEYBOARD_KEY_STATE_PRESSED
  2612. : WL_KEYBOARD_KEY_STATE_RELEASED;
  2613. /* mirror keyboard_handle_key: offer the keysym to the shell global-key hook first
  2614. * (Super/Escape toggles), and only forward to the focused client when unhandled */
  2615. uint32_t xkb_keycode = keycode + 8;
  2616. const xkb_keysym_t *syms;
  2617. int nsyms = xkb_state_key_get_syms(keyboard->xkb_state, xkb_keycode, &syms);
  2618. uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard);
  2619. bool handled = false;
  2620. if (pressed) {
  2621. for (int i = 0; i < nsyms; i++) {
  2622. if (server->cbs.key != NULL &&
  2623. server->cbs.key(server->ud, syms[i], modifiers, true)) {
  2624. handled = true;
  2625. }
  2626. }
  2627. }
  2628. if (!handled) {
  2629. wlr_seat_set_keyboard(server->seat, keyboard);
  2630. wlr_seat_keyboard_notify_key(server->seat, mjc_now_msec(), keycode, state);
  2631. }
  2632. }