extension.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. // Mutter has no layer-shell: turns the engine's per-output toplevels into desktop windows and reports the pointer
  2. // and per-monitor window states (WE 2.8 rules) a Wayland client can't see.
  3. import Clutter from 'gi://Clutter';
  4. import Cogl from 'gi://Cogl';
  5. import GObject from 'gi://GObject';
  6. import Gio from 'gi://Gio';
  7. import GLib from 'gi://GLib';
  8. import Meta from 'gi://Meta';
  9. import Shell from 'gi://Shell';
  10. import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
  11. import * as Main from 'resource:///org/gnome/shell/ui/main.js';
  12. import {WorkspaceBackground} from 'resource:///org/gnome/shell/ui/workspace.js';
  13. const APP_ID = 'linux-wallpaperengine';
  14. const TITLE_PREFIX = 'linux-wallpaperengine wallpaper ';
  15. const BUS_NAME = 'org.linuxwallpaperengine.Shell';
  16. const OBJECT_PATH = '/org/linuxwallpaperengine/Shell';
  17. const API_VERSION = 1;
  18. const FLAG_FOCUSED = 1;
  19. const FLAG_MAXIMIZED = 2;
  20. const FLAG_FULLSCREEN = 4;
  21. const INTERFACE = `
  22. <node>
  23. <interface name="org.linuxwallpaperengine.Shell">
  24. <property name="Version" type="u" access="read"/>
  25. <property name="Supported" type="b" access="read"/>
  26. <property name="NeedsHelper" type="b" access="read"/>
  27. <method name="SpawnHelper">
  28. <arg type="s" name="socket" direction="in"/>
  29. </method>
  30. <method name="GetState">
  31. <arg type="a(sdd)" name="pointer" direction="out"/>
  32. <arg type="a(ssu)" name="windows" direction="out"/>
  33. </method>
  34. <signal name="PointerMoved">
  35. <arg type="a(sdd)" name="pointer"/>
  36. </signal>
  37. <signal name="DesktopButton">
  38. <arg type="b" name="pressed"/>
  39. </signal>
  40. <signal name="WindowsChanged">
  41. <arg type="a(ssu)" name="windows"/>
  42. </signal>
  43. </interface>
  44. </node>`;
  45. // Same rounded work-area clip the overview gives workspace backgrounds; bounds and radius are monitor fractions.
  46. const CLIP_DECLARATIONS = `
  47. uniform vec4 bounds;
  48. uniform vec2 radius;
  49. `;
  50. const CLIP_CODE = `
  51. vec2 p = cogl_tex_coord_in[0].xy;
  52. float inside = step(bounds.x, p.x) * step(p.x, bounds.z) * step(bounds.y, p.y) * step(p.y, bounds.w);
  53. if (radius.x > 0.0 && radius.y > 0.0) {
  54. vec2 corner = clamp(p, bounds.xy + radius, bounds.zw - radius);
  55. inside *= 1.0 - smoothstep(0.96, 1.0, length((p - corner) / radius));
  56. }
  57. cogl_color_out *= inside;
  58. `;
  59. const OverviewClipEffect = GObject.registerClass(
  60. class OverviewClipEffect extends Shell.GLSLEffect {
  61. _init(params) {
  62. super._init(params);
  63. this._boundsLocation = this.get_uniform_location('bounds');
  64. this._radiusLocation = this.get_uniform_location('radius');
  65. }
  66. vfunc_build_pipeline() {
  67. // Cogl samples first, the clip only scales the result
  68. this.add_glsl_snippet(Cogl.SnippetHook.FRAGMENT, CLIP_DECLARATIONS, CLIP_CODE, false);
  69. }
  70. setClip(bounds, radius) {
  71. this.set_uniform_float(this._boundsLocation, 4, bounds);
  72. this.set_uniform_float(this._radiusLocation, 2, radius);
  73. this.queue_repaint();
  74. }
  75. });
  76. // 49+: set_type on any MetaWindow. 46-48: only via MetaWaylandClient for Shell-spawned clients (the helper).
  77. // 45: no Wayland desktop windows, the wallpaper stays lowest in the normal layer like DING.
  78. const CAN_SET_TYPE = typeof Meta.Window.prototype.set_type === 'function';
  79. const CAN_MAKE_DESKTOP = typeof Meta.WaylandClient?.prototype.make_desktop === 'function';
  80. const CAN_HIDE = typeof Meta.WaylandClient?.prototype.hide_from_window_list === 'function';
  81. const SUPPORTED = CAN_SET_TYPE || CAN_HIDE;
  82. const HELPER = 'linux-wallpaperengine-gnome-helper';
  83. // icon extensions using a bottom-layer normal window (DING on Ubuntu 46, its Gtk4 fork)
  84. const DESKTOP_ICON_APPS = ['com.rastersoft.ding', 'com.desktop.ding'];
  85. // windows can move under a still pointer, re-pick after this long
  86. const PICK_INTERVAL_US = 250000;
  87. function isMaximized(window) {
  88. const flags = window.get_maximize_flags ? window.get_maximize_flags() : window.get_maximized();
  89. return flags === Meta.MaximizeFlags.BOTH;
  90. }
  91. function cursorTracker() {
  92. return global.backend.get_cursor_tracker?.() ?? Meta.CursorTracker.get_for_display(global.display);
  93. }
  94. function appIdOf(window) {
  95. return window.get_sandboxed_app_id() || window.get_gtk_application_id() || window.get_wm_class() || '';
  96. }
  97. export default class WallpaperEngineExtension extends Extension {
  98. enable() {
  99. this._wallpapers = new Map();
  100. this._watched = new Map();
  101. this._windowsJson = '';
  102. this._windows = [];
  103. this._pointerIdle = 0;
  104. this._stateIdle = 0;
  105. this._dbus = Gio.DBusExportedObject.wrapJSObject(INTERFACE, this);
  106. this._dbus.export(Gio.DBus.session, OBJECT_PATH);
  107. this._nameId = Gio.bus_own_name_on_connection(Gio.DBus.session, BUS_NAME, Gio.BusNameOwnerFlags.NONE,
  108. null, null);
  109. this._displaySignals = [
  110. global.display.connect('window-created', (_display, window) => this._watch(window)),
  111. global.display.connect('notify::focus-window', () => this._queueState()),
  112. // a window moved to another monitor keeps its size
  113. global.display.connect('window-entered-monitor', () => this._queueState()),
  114. global.display.connect('restacked', () => {
  115. this._pickStale = true;
  116. this._pruneWallpapers();
  117. this._hideOrphanActors();
  118. this._keepBelowDesktopWindows();
  119. this._queueState();
  120. }),
  121. ];
  122. this._workspaceSignal = global.workspace_manager.connect('active-workspace-changed',
  123. () => this._queueState());
  124. this._monitorManager = global.backend.get_monitor_manager();
  125. this._monitorSignal = this._monitorManager.connect('monitors-changed', () => {
  126. for (const window of this._wallpapers.keys())
  127. this._place(window);
  128. this._queueState();
  129. this._queuePointer();
  130. });
  131. this._helperClients = new Set();
  132. this._raisedIds = new Map();
  133. this._shown = new Set();
  134. this._cursorTracker = cursorTracker();
  135. this._cursorSignal = this._cursorTracker.connect('position-invalidated', () => this._queuePointer());
  136. this._leftDown = false;
  137. this._leftForwarded = false;
  138. this._pointerOverDesktop = false;
  139. this._pickStale = true;
  140. this._pickX = this._pickY = -1;
  141. this._pickTime = 0;
  142. this._paintSignal = global.stage.connect('after-paint', () => this._pollLeftButton());
  143. this._overviewClones = new Map();
  144. this._overviewSignal = Main.overview.connect('showing', () => this._syncOverview());
  145. this._workareaSignal = global.display.connect('workareas-changed', () => this._syncOverview());
  146. for (const actor of global.get_window_actors())
  147. this._watch(actor.meta_window);
  148. this._queueState();
  149. }
  150. disable() {
  151. for (const id of [this._pointerIdle, this._stateIdle]) {
  152. if (id)
  153. GLib.source_remove(id);
  154. }
  155. this._pointerIdle = this._stateIdle = 0;
  156. Main.overview.disconnect(this._overviewSignal);
  157. global.display.disconnect(this._workareaSignal);
  158. for (const background of [...this._overviewClones.keys()])
  159. this._dropOverviewClone(background);
  160. this._overviewClones = null;
  161. this._cursorTracker.disconnect(this._cursorSignal);
  162. global.stage.disconnect(this._paintSignal);
  163. this._monitorManager.disconnect(this._monitorSignal);
  164. global.workspace_manager.disconnect(this._workspaceSignal);
  165. for (const id of this._displaySignals)
  166. global.display.disconnect(id);
  167. for (const [window, ids] of this._watched) {
  168. for (const id of ids)
  169. window.disconnect(id);
  170. }
  171. this._watched.clear();
  172. // otherwise they would be plain windows covering the monitor
  173. for (const window of this._wallpapers.keys())
  174. window.delete(global.display.get_current_time_roundtrip());
  175. this._wallpapers.clear();
  176. Gio.bus_unown_name(this._nameId);
  177. this._dbus.unexport();
  178. this._dbus = null;
  179. this._cursorTracker = this._monitorManager = null;
  180. for (const [window, id] of this._raisedIds)
  181. window.disconnect(id);
  182. this._helperClients = null;
  183. this._raisedIds = null;
  184. this._shown = null;
  185. }
  186. get Version() {
  187. return API_VERSION;
  188. }
  189. get Supported() {
  190. return SUPPORTED;
  191. }
  192. get NeedsHelper() {
  193. return !CAN_SET_TYPE && CAN_HIDE;
  194. }
  195. // Only ever runs our own helper; callers just pick an engine socket in the runtime folder (Flatpak calls come
  196. // through xdg-dbus-proxy, so the caller's pid is meaningless)
  197. SpawnHelperAsync([socket], invocation) {
  198. try {
  199. if (!this.NeedsHelper)
  200. throw new Error('this GNOME Shell makes desktop windows without the helper');
  201. const runtime = `${GLib.get_user_runtime_dir()}/`;
  202. const socketName = socket.startsWith(runtime) ? socket.slice(runtime.length) : '';
  203. if (!/^(app\/[^/]+\/)?lwe-gnome-(\d+_)?\d+\.sock$/.test(socketName))
  204. throw new Error(`refusing a helper for ${socket}`);
  205. const helper = GLib.build_filenamev([this.path, HELPER]);
  206. const launcher = new Gio.SubprocessLauncher({flags: Gio.SubprocessFlags.NONE});
  207. const client = Meta.WaylandClient.new(global.context, launcher);
  208. const process = client.spawnv(global.display, [helper, socket]);
  209. this._helperClients.add(client);
  210. process.wait_async(null, () => this._helperClients?.delete(client));
  211. invocation.return_value(null);
  212. } catch (error) {
  213. invocation.return_error_literal(Gio.DBusError, Gio.DBusError.ACCESS_DENIED, error.message);
  214. }
  215. }
  216. GetState() {
  217. return [this._pointer(), this._windows];
  218. }
  219. // fractions from the top left, stage and surface coords differ without scale-monitor-framebuffer
  220. _pointer() {
  221. const [x, y] = global.get_pointer();
  222. const result = [];
  223. for (const [window, connector] of this._wallpapers) {
  224. const frame = window.get_frame_rect();
  225. if (frame.width > 0 && frame.height > 0)
  226. result.push([connector, (x - frame.x) / frame.width, (y - frame.y) / frame.height]);
  227. }
  228. return result;
  229. }
  230. _watch(window) {
  231. if (this._watched.has(window))
  232. return;
  233. // title and app id may come after the window exists
  234. const check = () => {
  235. if (!this._wallpapers.has(window) && this._isWallpaper(window))
  236. this._adopt(window);
  237. this._queueState();
  238. };
  239. const ids = [
  240. window.connect('notify::wm-class', check),
  241. window.connect('notify::title', check),
  242. window.connect('notify::minimized', () => this._queueState()),
  243. window.connect('notify::maximized-horizontally', () => this._queueState()),
  244. window.connect('notify::maximized-vertically', () => this._queueState()),
  245. window.connect('notify::fullscreen', () => this._queueState()),
  246. window.connect('size-changed', () => {
  247. this._queueState();
  248. if (this._wallpapers.has(window))
  249. this._queuePointer();
  250. }),
  251. window.connect('workspace-changed', () => this._queueState()),
  252. window.connect('unmanaged', () => {
  253. for (const id of this._watched.get(window) ?? [])
  254. window.disconnect(id);
  255. this._watched.delete(window);
  256. this._raisedIds?.delete(window);
  257. this._shown?.delete(window);
  258. if (this._wallpapers.delete(window))
  259. this._syncOverview();
  260. this._queueState();
  261. }),
  262. ];
  263. this._watched.set(window, ids);
  264. check();
  265. }
  266. _isWallpaper(window) {
  267. return window.get_client_type() === Meta.WindowClientType.WAYLAND &&
  268. window.get_wm_class() === APP_ID &&
  269. (window.get_title() ?? '').startsWith(TITLE_PREFIX);
  270. }
  271. _adopt(window) {
  272. const connector = window.get_title().slice(TITLE_PREFIX.length);
  273. this._wallpapers.set(window, connector);
  274. // not fullscreen on the engine side (would hide the top bar), the monitor size comes from here
  275. if (CAN_SET_TYPE) {
  276. window.set_type(Meta.WindowType.DESKTOP);
  277. window.hide_from_window_list();
  278. } else {
  279. const client = [...this._helperClients].find(candidate => candidate.owns_window(window));
  280. if (!client) {
  281. console.error(`${this.uuid}: ${window.get_title()} is not from a helper connection, it stays a window`);
  282. return;
  283. }
  284. if (CAN_MAKE_DESKTOP) {
  285. client.make_desktop(window);
  286. } else {
  287. this._raisedIds.set(window, window.connect_after('raised', () => window.lower()));
  288. this._returnFocus(window, global.display.focus_window);
  289. }
  290. client.hide_from_window_list(window);
  291. }
  292. window.stick();
  293. this._place(window);
  294. const shownId = window.connect('shown', () => {
  295. window.disconnect(shownId);
  296. this._shown.add(window);
  297. this._place(window);
  298. this._keepBelowDesktopWindows();
  299. });
  300. this._queuePointer();
  301. this._syncOverview();
  302. }
  303. // The overview hides the window group, so a clone over each workspace background keeps it live
  304. _syncOverview() {
  305. if (!this._overviewClones)
  306. return;
  307. const found = new Set();
  308. const walk = actor => {
  309. if (actor instanceof WorkspaceBackground) {
  310. found.add(actor);
  311. this._syncWorkspaceBackground(actor);
  312. return;
  313. }
  314. for (const child of actor.get_children())
  315. walk(child);
  316. };
  317. walk(Main.layoutManager.overviewGroup);
  318. for (const background of [...this._overviewClones.keys()]) {
  319. if (!found.has(background))
  320. this._dropOverviewClone(background);
  321. }
  322. }
  323. _wallpaperActorOn(monitorIndex) {
  324. for (const window of this._wallpapers.keys()) {
  325. if (window.get_monitor() === monitorIndex && window.get_compositor_private())
  326. return window.get_compositor_private();
  327. }
  328. return null;
  329. }
  330. _syncWorkspaceBackground(background) {
  331. const source = SUPPORTED ? this._wallpaperActorOn(background._monitorIndex) : null;
  332. const existing = this._overviewClones.get(background);
  333. if (existing?.clone.source === source && source) {
  334. this._updateOverviewClip(background, existing);
  335. return;
  336. }
  337. if (existing)
  338. this._dropOverviewClone(background);
  339. if (!source)
  340. return;
  341. const clone = new Clutter.Clone({source, x_expand: true, y_expand: true});
  342. const effect = new OverviewClipEffect();
  343. clone.add_effect(effect);
  344. const group = background._backgroundGroup;
  345. group.add_child(clone);
  346. group.set_child_above_sibling(clone, null);
  347. const entry = {clone, effect, group, signals: [], content: null, contentSignal: 0};
  348. const manager = background._bgManager;
  349. entry.signals.push([manager, manager.connect('changed', () => {
  350. group.set_child_above_sibling(clone, null);
  351. this._updateOverviewClip(background, entry);
  352. })]);
  353. entry.signals.push([background, background.connect('destroy', () => this._dropOverviewClone(background, true))]);
  354. this._overviewClones.set(background, entry);
  355. this._updateOverviewClip(background, entry);
  356. }
  357. _updateOverviewClip(background, entry) {
  358. const content = background._bgManager?.backgroundActor?.content;
  359. if (!content)
  360. return;
  361. if (entry.content !== content) {
  362. if (entry.contentSignal)
  363. entry.content.disconnect(entry.contentSignal);
  364. entry.content = content;
  365. entry.contentSignal = content.connect('notify::rounded-clip-radius',
  366. () => this._updateOverviewClip(background, entry));
  367. }
  368. const monitor = Main.layoutManager.monitors[background._monitorIndex];
  369. const area = background._workarea;
  370. if (!monitor || !area || monitor.width <= 0 || monitor.height <= 0)
  371. return;
  372. const left = (area.x - monitor.x) / monitor.width;
  373. const top = (area.y - monitor.y) / monitor.height;
  374. const radius = content.rounded_clip_radius;
  375. entry.effect.setClip(
  376. [left, top, left + area.width / monitor.width, top + area.height / monitor.height],
  377. [radius / monitor.width, radius / monitor.height]);
  378. }
  379. _dropOverviewClone(background, destroyed = false) {
  380. const entry = this._overviewClones?.get(background);
  381. if (!entry)
  382. return;
  383. this._overviewClones.delete(background);
  384. if (destroyed)
  385. return;
  386. for (const [object, id] of entry.signals)
  387. object.disconnect(id);
  388. if (entry.contentSignal)
  389. entry.content.disconnect(entry.contentSignal);
  390. entry.clone.destroy();
  391. }
  392. // DING shares the wallpaper's layer and later windows land on top; keep the wallpaper lowest, under the icons
  393. _keepBelowDesktopWindows() {
  394. const stack = global.display.sort_windows_by_stacking(global.display.list_all_windows());
  395. for (const wallpaper of this._wallpapers.keys()) {
  396. const layer = wallpaper.get_layer();
  397. const index = stack.indexOf(wallpaper);
  398. // lowering re-emits restacked
  399. if (stack.slice(0, Math.max(index, 0))
  400. .some(window => window.get_layer() === layer && !this._wallpapers.has(window)))
  401. wallpaper.lower();
  402. }
  403. }
  404. // Shell 46 can free a wallpaper window without unmanaging it, its actor then stays in the window group with the last frame
  405. _hideOrphanActors() {
  406. for (const actor of global.window_group.get_children()) {
  407. const window = actor.meta_window;
  408. if (!window || !actor.visible || window.get_compositor_private() === actor || !this._isWallpaper(window))
  409. continue;
  410. actor.hide();
  411. }
  412. }
  413. // GNOME 45 drops a gone client's window without 'unmanaged' (Shell 45.2)
  414. _pruneWallpapers() {
  415. const live = new Set(global.display.list_all_windows());
  416. for (const window of [...this._wallpapers.keys()]) {
  417. if (!this._shown.has(window) || live.has(window))
  418. continue;
  419. for (const id of this._watched.get(window) ?? [])
  420. window.disconnect(id);
  421. this._watched.delete(window);
  422. this._wallpapers.delete(window);
  423. this._shown.delete(window);
  424. this._raisedIds.delete(window);
  425. this._syncOverview();
  426. }
  427. }
  428. _returnFocus(window, previous) {
  429. const shownId = window.connect('shown', () => {
  430. window.disconnect(shownId);
  431. if (window.has_focus() && previous && previous !== window && !previous.minimized)
  432. previous.activate(global.display.get_current_time_roundtrip());
  433. });
  434. }
  435. _place(window) {
  436. const connector = this._wallpapers.get(window);
  437. const monitor = this._monitorManager.get_monitor_for_connector(connector);
  438. if (monitor < 0)
  439. return;
  440. const area = global.display.get_monitor_geometry(monitor);
  441. window.move_resize_frame(false, area.x, area.y, area.width, area.height);
  442. }
  443. // Clicks on desktop icons never reach our surface, but WE polls the left button each frame (sub_14010D9B0) and
  444. // counts a press over the icon view. Stage events miss Wayland client input, so poll once per painted frame too.
  445. _pollLeftButton() {
  446. if (this._wallpapers.size === 0)
  447. return;
  448. const [, , mods] = global.get_pointer();
  449. const down = (mods & Clutter.ModifierType.BUTTON1_MASK) !== 0;
  450. if (down === this._leftDown) {
  451. if (!down)
  452. this._updatePointerOverDesktop();
  453. return;
  454. }
  455. this._leftDown = down;
  456. // pick before the press, icons react to it (DING starts a rubber band)
  457. if (down && !Main.overview.visible && this._pointerOverDesktop) {
  458. this._leftForwarded = true;
  459. this._dbus?.emit_signal('DesktopButton', new GLib.Variant('(b)', [true]));
  460. } else if (!down && this._leftForwarded) {
  461. this._leftForwarded = false;
  462. this._dbus?.emit_signal('DesktopButton', new GLib.Variant('(b)', [false]));
  463. }
  464. }
  465. // runs every frame, a pick walks the whole stage
  466. _updatePointerOverDesktop() {
  467. const [x, y] = global.get_pointer();
  468. const now = GLib.get_monotonic_time();
  469. if (!this._pickStale && x === this._pickX && y === this._pickY && now - this._pickTime < PICK_INTERVAL_US)
  470. return;
  471. this._pickStale = false;
  472. this._pickX = x;
  473. this._pickY = y;
  474. this._pickTime = now;
  475. this._pointerOverDesktop = this._overOtherDesktopWindow(x, y);
  476. }
  477. _overOtherDesktopWindow(x, y) {
  478. let actor = global.stage.get_actor_at_pos(Clutter.PickMode.REACTIVE, x, y);
  479. while (actor && !(actor instanceof Meta.WindowActor))
  480. actor = actor.get_parent();
  481. const window = actor?.get_meta_window();
  482. return !!window && this._isDesktop(window);
  483. }
  484. // other desktop windows and icon windows
  485. _isDesktop(window) {
  486. if (this._wallpapers.has(window))
  487. return false;
  488. return window.get_window_type() === Meta.WindowType.DESKTOP ||
  489. DESKTOP_ICON_APPS.includes(window.get_gtk_application_id());
  490. }
  491. _queuePointer() {
  492. if (this._pointerIdle || this._wallpapers.size === 0)
  493. return;
  494. this._pointerIdle = GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
  495. this._pointerIdle = 0;
  496. this._dbus?.emit_signal('PointerMoved', new GLib.Variant('(a(sdd))', [this._pointer()]));
  497. return GLib.SOURCE_REMOVE;
  498. });
  499. }
  500. _queueState() {
  501. if (this._stateIdle)
  502. return;
  503. this._stateIdle = GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
  504. this._stateIdle = 0;
  505. this._updateState();
  506. return GLib.SOURCE_REMOVE;
  507. });
  508. }
  509. // GNOME 46 can't list logical monitors, only outputs with a wallpaper are reported
  510. _connectorsOf(monitorIndex) {
  511. return [...new Set(this._wallpapers.values())]
  512. .filter(connector => this._monitorManager.get_monitor_for_connector(connector) === monitorIndex);
  513. }
  514. // WE 2.8 (sub_14003D050 / sub_14003BBA0): visible, unminimized windows on the current desktop. Maximized marks its
  515. // monitor; the foreground root window marks focused, and fullscreen if not maximized and at least monitor-sized.
  516. _collectWindows() {
  517. const workspace = global.workspace_manager.get_active_workspace();
  518. const focus = global.display.focus_window?.find_root_ancestor() ?? null;
  519. const windows = [];
  520. for (const actor of global.get_window_actors()) {
  521. const window = actor.meta_window;
  522. if (this._wallpapers.has(window) || window.minimized || !window.showing_on_its_workspace() ||
  523. !window.located_on_workspace(workspace) || window.is_override_redirect())
  524. continue;
  525. if (window.get_window_type() === Meta.WindowType.DOCK || this._isDesktop(window))
  526. continue;
  527. const maximized = isMaximized(window);
  528. let flags = maximized ? FLAG_MAXIMIZED : 0;
  529. if (window === focus) {
  530. flags |= FLAG_FOCUSED;
  531. const frame = window.get_frame_rect();
  532. const area = global.display.get_monitor_geometry(window.get_monitor());
  533. if (!maximized && (window.is_fullscreen() || (frame.width >= area.width && frame.height >= area.height)))
  534. flags |= FLAG_FULLSCREEN;
  535. }
  536. if (flags === 0)
  537. continue;
  538. for (const connector of this._connectorsOf(window.get_monitor()))
  539. windows.push([appIdOf(window), connector, flags]);
  540. }
  541. return windows;
  542. }
  543. _updateState() {
  544. const windows = this._collectWindows();
  545. const json = JSON.stringify(windows);
  546. if (json === this._windowsJson)
  547. return;
  548. this._windowsJson = json;
  549. this._windows = windows;
  550. this._dbus?.emit_signal('WindowsChanged', new GLib.Variant('(a(ssu))', [windows]));
  551. }
  552. }