import Gio from 'gi://Gio'; import Meta from 'gi://Meta'; import Shell from 'gi://Shell'; const BUS = 'io.qvac.Jarvis.Shell'; const PATH = '/io/qvac/Jarvis/Shell'; const XML = ``; const describe = (window) => ({ id: Number(window.get_id?.() || 0), title: window.get_title?.() || '', wm_class: window.get_wm_class?.() || '', pid: Number(window.get_pid?.() || 0), rect: (() => { const r = window.get_frame_rect?.(); return r ? [r.x, r.y, r.width, r.height] : null; })(), buffer_rect: (() => { const r = window.get_buffer_rect?.(); return r ? [r.x, r.y, r.width, r.height] : null; })(), focused: window === global.display.get_focus_window?.() }); export function installShellService() { const implementation = { ListWindows() { const windows = global.display.get_tab_list(Meta.TabList.NORMAL_ALL, null).map(describe); return JSON.stringify(windows); }, FocusedWindow() { const window = global.display.get_focus_window?.(); return JSON.stringify(window ? describe(window) : null); }, FocusWindow(id) { const window = global.display.get_tab_list(Meta.TabList.NORMAL_ALL, null).find((candidate) => Number(candidate.get_id?.()) === Number(id)); if (!window) throw new Error('window not found'); window.activate(global.get_current_time()); }, async Screenshot(filename) { const file = Gio.File.new_for_path(filename); const stream = file.replace(null, false, Gio.FileCreateFlags.REPLACE_DESTINATION, null); try { const screenshot = new Shell.Screenshot(); await screenshot.screenshot(false, stream); } finally { stream.close(null); } return filename; }, }; const ownerId = Gio.bus_own_name(Gio.BusType.SESSION, BUS, Gio.BusNameOwnerFlags.NONE, (connection) => { const exported = Gio.DBusExportedObject.wrapJSObject(XML, implementation); exported.export(connection, PATH); implementation._exported = exported; }, null, () => {}); return () => { implementation._exported?.unexport(); Gio.bus_unown_name(ownerId); }; }