extension.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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._keepBelowDesktopWindows();
  118. this._queueState();
  119. }),
  120. ];
  121. this._workspaceSignal = global.workspace_manager.connect('active-workspace-changed',
  122. () => this._queueState());
  123. this._monitorManager = global.backend.get_monitor_manager();
  124. this._monitorSignal = this._monitorManager.connect('monitors-changed', () => {
  125. for (const window of this._wallpapers.keys())
  126. this._place(window);
  127. this._queueState();
  128. this._queuePointer();
  129. });
  130. this._helperClients = new Set();
  131. this._raisedIds = new Map();
  132. this._shown = new Set();
  133. this._cursorTracker = cursorTracker();
  134. this._cursorSignal = this._cursorTracker.connect('position-invalidated', () => this._queuePointer());
  135. this._leftDown = false;
  136. this._leftForwarded = false;
  137. this._pointerOverDesktop = false;
  138. this._pickStale = true;
  139. this._pickX = this._pickY = -1;
  140. this._pickTime = 0;
  141. this._paintSignal = global.stage.connect('after-paint', () => this._pollLeftButton());
  142. this._overviewClones = new Map();
  143. this._overviewSignal = Main.overview.connect('showing', () => this._syncOverview());
  144. this._workareaSignal = global.display.connect('workareas-changed', () => this._syncOverview());
  145. for (const actor of global.get_window_actors())
  146. this._watch(actor.meta_window);
  147. this._queueState();
  148. }
  149. disable() {
  150. for (const id of [this._pointerIdle, this._stateIdle]) {
  151. if (id)
  152. GLib.source_remove(id);
  153. }
  154. this._pointerIdle = this._stateIdle = 0;
  155. Main.overview.disconnect(this._overviewSignal);
  156. global.display.disconnect(this._workareaSignal);
  157. for (const background of [...this._overviewClones.keys()])
  158. this._dropOverviewClone(background);
  159. this._overviewClones = null;
  160. this._cursorTracker.disconnect(this._cursorSignal);
  161. global.stage.disconnect(this._paintSignal);
  162. this._monitorManager.disconnect(this._monitorSignal);
  163. global.workspace_manager.disconnect(this._workspaceSignal);
  164. for (const id of this._displaySignals)
  165. global.display.disconnect(id);
  166. for (const [window, ids] of this._watched) {
  167. for (const id of ids)
  168. window.disconnect(id);
  169. }
  170. this._watched.clear();
  171. // otherwise they would be plain windows covering the monitor
  172. for (const window of this._wallpapers.keys())
  173. window.delete(global.display.get_current_time_roundtrip());
  174. this._wallpapers.clear();
  175. Gio.bus_unown_name(this._nameId);
  176. this._dbus.unexport();
  177. this._dbus = null;
  178. this._cursorTracker = this._monitorManager = null;
  179. for (const [window, id] of this._raisedIds)
  180. window.disconnect(id);
  181. this._helperClients = null;
  182. this._raisedIds = null;
  183. this._shown = null;
  184. }
  185. get Version() {
  186. return API_VERSION;
  187. }
  188. get Supported() {
  189. return SUPPORTED;
  190. }
  191. get NeedsHelper() {
  192. return !CAN_SET_TYPE && CAN_HIDE;
  193. }
  194. // Only ever runs our own helper; callers just pick an engine socket in the runtime folder (Flatpak calls come
  195. // through xdg-dbus-proxy, so the caller's pid is meaningless)
  196. SpawnHelperAsync([socket], invocation) {
  197. try {
  198. if (!this.NeedsHelper)
  199. throw new Error('this GNOME Shell makes desktop windows without the helper');
  200. const runtime = `${GLib.get_user_runtime_dir()}/`;
  201. const socketName = socket.startsWith(runtime) ? socket.slice(runtime.length) : '';
  202. if (!/^(app\/[^/]+\/)?lwe-gnome-\d+\.sock$/.test(socketName))
  203. throw new Error(`refusing a helper for ${socket}`);
  204. const helper = GLib.build_filenamev([this.path, HELPER]);
  205. const launcher = new Gio.SubprocessLauncher({flags: Gio.SubprocessFlags.NONE});
  206. const client = Meta.WaylandClient.new(global.context, launcher);
  207. const process = client.spawnv(global.display, [helper, socket]);
  208. this._helperClients.add(client);
  209. process.wait_async(null, () => this._helperClients?.delete(client));
  210. invocation.return_value(null);
  211. } catch (error) {
  212. invocation.return_error_literal(Gio.DBusError, Gio.DBusError.ACCESS_DENIED, error.message);
  213. }
  214. }
  215. GetState() {
  216. return [this._pointer(), this._windows];
  217. }
  218. // fractions from the top left, stage and surface coords differ without scale-monitor-framebuffer
  219. _pointer() {
  220. const [x, y] = global.get_pointer();
  221. const result = [];
  222. for (const [window, connector] of this._wallpapers) {
  223. const frame = window.get_frame_rect();
  224. if (frame.width > 0 && frame.height > 0)
  225. result.push([connector, (x - frame.x) / frame.width, (y - frame.y) / frame.height]);
  226. }
  227. return result;
  228. }
  229. _watch(window) {
  230. if (this._watched.has(window))
  231. return;
  232. // title and app id may come after the window exists
  233. const check = () => {
  234. if (!this._wallpapers.has(window) && this._isWallpaper(window))
  235. this._adopt(window);
  236. this._queueState();
  237. };
  238. const ids = [
  239. window.connect('notify::wm-class', check),
  240. window.connect('notify::title', check),
  241. window.connect('notify::minimized', () => this._queueState()),
  242. window.connect('notify::maximized-horizontally', () => this._queueState()),
  243. window.connect('notify::maximized-vertically', () => this._queueState()),
  244. window.connect('notify::fullscreen', () => this._queueState()),
  245. window.connect('size-changed', () => {
  246. this._queueState();
  247. if (this._wallpapers.has(window))
  248. this._queuePointer();
  249. }),
  250. window.connect('workspace-changed', () => this._queueState()),
  251. window.connect('unmanaged', () => {
  252. for (const id of this._watched.get(window) ?? [])
  253. window.disconnect(id);
  254. this._watched.delete(window);
  255. this._raisedIds?.delete(window);
  256. this._shown?.delete(window);
  257. if (this._wallpapers.delete(window))
  258. this._syncOverview();
  259. this._queueState();
  260. }),
  261. ];
  262. this._watched.set(window, ids);
  263. check();
  264. }
  265. _isWallpaper(window) {
  266. return window.get_client_type() === Meta.WindowClientType.WAYLAND &&
  267. window.get_wm_class() === APP_ID &&
  268. (window.get_title() ?? '').startsWith(TITLE_PREFIX);
  269. }
  270. _adopt(window) {
  271. const connector = window.get_title().slice(TITLE_PREFIX.length);
  272. this._wallpapers.set(window, connector);
  273. // not fullscreen on the engine side (would hide the top bar), the monitor size comes from here
  274. if (CAN_SET_TYPE) {
  275. window.set_type(Meta.WindowType.DESKTOP);
  276. window.hide_from_window_list();
  277. } else {
  278. const client = [...this._helperClients].find(candidate => candidate.owns_window(window));
  279. if (!client) {
  280. console.error(`${this.uuid}: ${window.get_title()} is not from a helper connection, it stays a window`);
  281. return;
  282. }
  283. if (CAN_MAKE_DESKTOP) {
  284. client.make_desktop(window);
  285. } else {
  286. this._raisedIds.set(window, window.connect_after('raised', () => window.lower()));
  287. this._returnFocus(window, global.display.focus_window);
  288. }
  289. client.hide_from_window_list(window);
  290. }
  291. window.stick();
  292. // again when shown, Mutter places new windows on the current monitor
  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. // new background actors go on top
  350. entry.signals.push([manager, manager.connect('changed', () => {
  351. group.set_child_above_sibling(clone, null);
  352. this._updateOverviewClip(background, entry);
  353. })]);
  354. entry.signals.push([background, background.connect('destroy', () => this._dropOverviewClone(background, true))]);
  355. this._overviewClones.set(background, entry);
  356. this._updateOverviewClip(background, entry);
  357. }
  358. _updateOverviewClip(background, entry) {
  359. const content = background._bgManager?.backgroundActor?.content;
  360. if (!content)
  361. return;
  362. if (entry.content !== content) {
  363. if (entry.contentSignal)
  364. entry.content.disconnect(entry.contentSignal);
  365. entry.content = content;
  366. entry.contentSignal = content.connect('notify::rounded-clip-radius',
  367. () => this._updateOverviewClip(background, entry));
  368. }
  369. const monitor = Main.layoutManager.monitors[background._monitorIndex];
  370. const area = background._workarea;
  371. if (!monitor || !area || monitor.width <= 0 || monitor.height <= 0)
  372. return;
  373. const left = (area.x - monitor.x) / monitor.width;
  374. const top = (area.y - monitor.y) / monitor.height;
  375. const radius = content.rounded_clip_radius;
  376. entry.effect.setClip(
  377. [left, top, left + area.width / monitor.width, top + area.height / monitor.height],
  378. [radius / monitor.width, radius / monitor.height]);
  379. }
  380. // the workspace went away with clone and background
  381. _dropOverviewClone(background, destroyed = false) {
  382. const entry = this._overviewClones?.get(background);
  383. if (!entry)
  384. return;
  385. this._overviewClones.delete(background);
  386. if (destroyed)
  387. return;
  388. for (const [object, id] of entry.signals)
  389. object.disconnect(id);
  390. if (entry.contentSignal)
  391. entry.content.disconnect(entry.contentSignal);
  392. entry.clone.destroy();
  393. }
  394. // DING shares the wallpaper's layer and later windows land on top; keep the wallpaper lowest, under the icons
  395. _keepBelowDesktopWindows() {
  396. const stack = global.display.sort_windows_by_stacking(global.display.list_all_windows());
  397. for (const wallpaper of this._wallpapers.keys()) {
  398. const layer = wallpaper.get_layer();
  399. const index = stack.indexOf(wallpaper);
  400. // lowering re-emits restacked
  401. if (stack.slice(0, Math.max(index, 0))
  402. .some(window => window.get_layer() === layer && !this._wallpapers.has(window)))
  403. wallpaper.lower();
  404. }
  405. }
  406. // GNOME 45 drops a gone client's window without 'unmanaged' (Shell 45.2)
  407. _pruneWallpapers() {
  408. const live = new Set(global.display.list_all_windows());
  409. for (const window of [...this._wallpapers.keys()]) {
  410. if (!this._shown.has(window) || live.has(window))
  411. continue;
  412. for (const id of this._watched.get(window) ?? [])
  413. window.disconnect(id);
  414. this._watched.delete(window);
  415. this._wallpapers.delete(window);
  416. this._shown.delete(window);
  417. this._raisedIds.delete(window);
  418. this._syncOverview();
  419. }
  420. }
  421. _returnFocus(window, previous) {
  422. const shownId = window.connect('shown', () => {
  423. window.disconnect(shownId);
  424. if (window.has_focus() && previous && previous !== window && !previous.minimized)
  425. previous.activate(global.display.get_current_time_roundtrip());
  426. });
  427. }
  428. _place(window) {
  429. const connector = this._wallpapers.get(window);
  430. const monitor = this._monitorManager.get_monitor_for_connector(connector);
  431. if (monitor < 0)
  432. return;
  433. const area = global.display.get_monitor_geometry(monitor);
  434. window.move_resize_frame(false, area.x, area.y, area.width, area.height);
  435. }
  436. // Clicks on desktop icons never reach our surface, but WE polls the left button each frame (sub_14010D9B0) and
  437. // counts a press over the icon view. Stage events miss Wayland client input, so poll once per painted frame too.
  438. _pollLeftButton() {
  439. if (this._wallpapers.size === 0)
  440. return;
  441. const [, , mods] = global.get_pointer();
  442. const down = (mods & Clutter.ModifierType.BUTTON1_MASK) !== 0;
  443. if (down === this._leftDown) {
  444. if (!down)
  445. this._updatePointerOverDesktop();
  446. return;
  447. }
  448. this._leftDown = down;
  449. // pick before the press, icons react to it (DING starts a rubber band)
  450. if (down && !Main.overview.visible && this._pointerOverDesktop) {
  451. this._leftForwarded = true;
  452. this._dbus?.emit_signal('DesktopButton', new GLib.Variant('(b)', [true]));
  453. } else if (!down && this._leftForwarded) {
  454. this._leftForwarded = false;
  455. this._dbus?.emit_signal('DesktopButton', new GLib.Variant('(b)', [false]));
  456. }
  457. }
  458. // runs every frame, a pick walks the whole stage
  459. _updatePointerOverDesktop() {
  460. const [x, y] = global.get_pointer();
  461. const now = GLib.get_monotonic_time();
  462. if (!this._pickStale && x === this._pickX && y === this._pickY && now - this._pickTime < PICK_INTERVAL_US)
  463. return;
  464. this._pickStale = false;
  465. this._pickX = x;
  466. this._pickY = y;
  467. this._pickTime = now;
  468. this._pointerOverDesktop = this._overOtherDesktopWindow(x, y);
  469. }
  470. _overOtherDesktopWindow(x, y) {
  471. let actor = global.stage.get_actor_at_pos(Clutter.PickMode.REACTIVE, x, y);
  472. while (actor && !(actor instanceof Meta.WindowActor))
  473. actor = actor.get_parent();
  474. const window = actor?.get_meta_window();
  475. return !!window && this._isDesktop(window);
  476. }
  477. // other desktop windows and icon windows
  478. _isDesktop(window) {
  479. if (this._wallpapers.has(window))
  480. return false;
  481. return window.get_window_type() === Meta.WindowType.DESKTOP ||
  482. DESKTOP_ICON_APPS.includes(window.get_gtk_application_id());
  483. }
  484. _queuePointer() {
  485. if (this._pointerIdle || this._wallpapers.size === 0)
  486. return;
  487. // one signal per main loop pass
  488. this._pointerIdle = GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
  489. this._pointerIdle = 0;
  490. this._dbus?.emit_signal('PointerMoved', new GLib.Variant('(a(sdd))', [this._pointer()]));
  491. return GLib.SOURCE_REMOVE;
  492. });
  493. }
  494. _queueState() {
  495. if (this._stateIdle)
  496. return;
  497. this._stateIdle = GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
  498. this._stateIdle = 0;
  499. this._updateState();
  500. return GLib.SOURCE_REMOVE;
  501. });
  502. }
  503. // GNOME 46 can't list logical monitors, only outputs with a wallpaper are reported
  504. _connectorsOf(monitorIndex) {
  505. return [...new Set(this._wallpapers.values())]
  506. .filter(connector => this._monitorManager.get_monitor_for_connector(connector) === monitorIndex);
  507. }
  508. // WE 2.8 (sub_14003D050 / sub_14003BBA0): visible, unminimized windows on the current desktop. Maximized marks its
  509. // monitor; the foreground root window marks focused, and fullscreen if not maximized and at least monitor-sized.
  510. _collectWindows() {
  511. const workspace = global.workspace_manager.get_active_workspace();
  512. const focus = global.display.focus_window?.find_root_ancestor() ?? null;
  513. const windows = [];
  514. for (const actor of global.get_window_actors()) {
  515. const window = actor.meta_window;
  516. if (this._wallpapers.has(window) || window.minimized || !window.showing_on_its_workspace() ||
  517. !window.located_on_workspace(workspace) || window.is_override_redirect())
  518. continue;
  519. if (window.get_window_type() === Meta.WindowType.DOCK || this._isDesktop(window))
  520. continue;
  521. const maximized = isMaximized(window);
  522. let flags = maximized ? FLAG_MAXIMIZED : 0;
  523. if (window === focus) {
  524. flags |= FLAG_FOCUSED;
  525. const frame = window.get_frame_rect();
  526. const area = global.display.get_monitor_geometry(window.get_monitor());
  527. if (!maximized && (window.is_fullscreen() || (frame.width >= area.width && frame.height >= area.height)))
  528. flags |= FLAG_FULLSCREEN;
  529. }
  530. if (flags === 0)
  531. continue;
  532. for (const connector of this._connectorsOf(window.get_monitor()))
  533. windows.push([appIdOf(window), connector, flags]);
  534. }
  535. return windows;
  536. }
  537. _updateState() {
  538. const windows = this._collectWindows();
  539. const json = JSON.stringify(windows);
  540. if (json === this._windowsJson)
  541. return;
  542. this._windowsJson = json;
  543. this._windows = windows;
  544. this._dbus?.emit_signal('WindowsChanged', new GLib.Variant('(a(ssu))', [windows]));
  545. }
  546. }