import Gio from "gi://Gio" import GioUnix from "gi://GioUnix" import GLib from "gi://GLib" const AGENT_ID = "quadshell" const AGENT_PATH = "/org/freedesktop/GeoClue2/Agent" const GEOCLUE = "org.freedesktop.GeoClue2" const MANAGER = `${GEOCLUE}.Manager` const MANAGER_PATH = "/org/freedesktop/GeoClue2/Manager" const ACCURACY_NONE = 0 const ACCURACY_EXACT = 8 const INTERFACE = ` ` const stateDir = GLib.build_filenamev([GLib.get_user_state_dir(), "quadshell"]) const stateFile = GLib.build_filenamev([stateDir, "location"]) let enabled = true try { const [, contents] = GLib.file_get_contents(stateFile) enabled = new TextDecoder().decode(contents).trim() !== "off" } catch { // Not set yet } const bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, null) const loop = new GLib.MainLoop(null, false) const stdout = new Gio.DataOutputStream({ base_stream: new GioUnix.OutputStream({ fd: 1 }), }) function report(key, value) { stdout.put_string(`${key} ${value ? 1 : 0}\n`, null) } const maxAccuracy = () => (enabled ? ACCURACY_EXACT : ACCURACY_NONE) const agent = Gio.DBusExportedObject.wrapJSObject(INTERFACE, { AuthorizeApp(_desktopId, requested) { return [enabled, Math.min(requested, maxAccuracy())] }, get MaxAccuracyLevel() { return maxAccuracy() }, }) function setEnabled(on) { if (on !== enabled) { enabled = on GLib.mkdir_with_parents(stateDir, 0o755) GLib.file_set_contents(stateFile, on ? "on" : "off") agent.emit_property_changed( "MaxAccuracyLevel", GLib.Variant.new_uint32(maxAccuracy()), ) } report("enabled", enabled) } function onGeoclueAppeared() { bus.call( GEOCLUE, MANAGER_PATH, MANAGER, "AddAgent", new GLib.Variant("(s)", [AGENT_ID]), null, Gio.DBusCallFlags.NONE, -1, null, (conn, result) => { try { conn.call_finish(result) } catch (e) { printerr( `GeoClue didn't accept the agent, is ${AGENT_ID} in its whitelist? ${e.message}`, ) report("available", false) } }, ) bus.call( GEOCLUE, MANAGER_PATH, "org.freedesktop.DBus.Properties", "Get", new GLib.Variant("(ss)", [MANAGER, "InUse"]), new GLib.VariantType("(v)"), Gio.DBusCallFlags.NONE, -1, null, (conn, result) => { try { report("inUse", conn.call_finish(result).recursiveUnpack()[0]) } catch { // It went away again } }, ) } const stdin = new Gio.DataInputStream({ base_stream: new GioUnix.InputStream({ fd: 0 }), }) function readLine() { stdin.read_line_async(GLib.PRIORITY_DEFAULT, null, (stream, result) => { const [line] = stream.read_line_finish_utf8(result) if (line === null) { loop.quit() return } if (line.trim() === "on" || line.trim() === "off") setEnabled(line.trim() === "on") readLine() }) } agent.export(bus, AGENT_PATH) bus.signal_subscribe( GEOCLUE, "org.freedesktop.DBus.Properties", "PropertiesChanged", MANAGER_PATH, MANAGER, Gio.DBusSignalFlags.NONE, (_conn, _sender, _path, _interface, _signal, params) => { const [, changed] = params.recursiveUnpack() if ("InUse" in changed) report("inUse", changed.InUse) }, ) Gio.bus_watch_name_on_connection( bus, GEOCLUE, Gio.BusNameWatcherFlags.NONE, onGeoclueAppeared, () => report("inUse", false), ) readLine() report("enabled", enabled) report("available", true) loop.run()