Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions bin/omarchy-shell
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,50 @@ if [[ $1 == "shell" && ( $2 == "summon" || $2 == "toggle" ) ]] && (( $# == 3 ));
set -- "$1" "$2" "$3" "{}"
fi

# The shell answers on its own socket first: socat starts in ~5ms where a qs
# ipc client takes ~45ms. Fields are split by unit separators and the request
# ends with a record separator, so an argument holding either goes through qs
# ipc instead. The shell replies "OK" with the output once the call has run,
# or "SKIP" when it ran nothing (no such target or function, the wrong number
# of arguments), and qs ipc then gives its exact answer. A timeout after the
# request is out is reported, not retried: the call may already have run.
# Like qs ipc, the socket belongs to one shell, the one running this config on
# this display, and the shell derives its name from the same two values.
socket_id=$(printf '%s\n%s' "$OMARCHY_PATH/shell" "${WAYLAND_DISPLAY:-}" | md5sum)
socket="${XDG_RUNTIME_DIR:-/run/user/$UID}/omarchy-shell-${socket_id:0:16}.sock"
request="$*"
if [[ -S $socket && $request != *$'\x1e'* && $request != *$'\x1f'* ]]; then
saved_ifs=$IFS
IFS=$'\x1f'
request="$*"
IFS=$saved_ifs
# socat waits as long as the shell takes; the timeout alone bounds the call.
# Its own errors land in the reply too, so a connection it could not make
# is told apart from one that failed after the request went out.
reply=$(printf '%s\x1e' "$request" | timeout --kill-after=1s "${OMARCHY_SHELL_IPC_TIMEOUT:-2s}" socat -t 86400 - "UNIX-CONNECT:$socket" 2>&1)

# An answer counts only once its record separator arrived: a reply cut off
# partway must not pass for the whole of it.
if [[ $reply == OK$'\x1f'*$'\x1e'* ]]; then
output=${reply#OK$'\x1f'}
output=${output%$'\x1e'*}
# Match qs ipc's path, whose $(...) drops trailing newlines.
while [[ $output == *$'\n' ]]; do output=${output%$'\n'}; done
if (( !QUIET )) && [[ -n $output ]]; then
echo "$output"
fi
exit 0
elif [[ $reply == "SKIP"$'\x1e'* ]]; then
: # The shell ran nothing; qs ipc gives its exact answer below.
elif [[ $reply != OK* && $reply == *" E connect("* ]]; then
: # socat never connected, a stale socket; qs ipc answers below.
else
# Out of time, cut off, or failed after the request went out: the call
# may have run, so it is reported rather than retried.
fail "omarchy-shell is not responding"
fi
fi

# The -- keeps function names that shadow qs subcommands (e.g. show) as
# positionals. qs reports connection failures with a nonzero exit, but IPC-level
# failures (unknown target/function, bad arguments) go to stdout with exit 0.
Expand Down
77 changes: 77 additions & 0 deletions shell/Commons/IpcRegistry.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Io

// Every ShellIpc handler, so the shell can answer an omarchy-shell call over
// its own socket instead of through a qs ipc client started per call. It
// answers only what qs ipc would: the first live, enabled handler for a
// target, and only the functions that handler declares. Those are allowed by
// name, never inferred from what a function call happens to reach: QObject
// methods such as destroy() are callable but not enumerable.
Singleton {
id: root

property var handlers: []

// A bare handler's own functions (signals and hooks such as targetChanged),
// which qs ipc never exposes and neither may the socket.
readonly property var builtins: functionNames(bareHandler)

IpcHandler {
id: bareHandler
enabled: false
}

function functionNames(object) {
var names = []
for (var key in object) {
if (typeof object[key] === "function") names.push(key)
}
return names
}

function register(handler) {
if (handlers.indexOf(handler) === -1) handlers.push(handler)
}

function unregister(handler) {
var index = handlers.indexOf(handler)
if (index !== -1) handlers.splice(index, 1)
}

function handlerFor(target) {
for (var i = 0; i < handlers.length; i++) {
var handler = handlers[i]
if (handler && handler.enabled && handler.target === target) return handler
}
return null
}

// { ran: true, output } once the function ran; { ran: false } when it did
// not (unknown target or function, wrong argument count), so the caller
// can ask qs ipc for its exact answer without running anything twice.
// The functions a handler declares: what it enumerates beyond a bare
// handler, less property change signals.
function declaredFunctions(handler) {
return functionNames(handler).filter(function(name) {
return builtins.indexOf(name) === -1 && !/Changed$/.test(name)
})
}

function call(target, method, args) {
var handler = handlerFor(target)
if (!handler) return { ran: false }
if (declaredFunctions(handler).indexOf(method) === -1) return { ran: false }
if (handler[method].length !== args.length) return { ran: false }

var result
try {
result = handler[method].apply(handler, args)
} catch (error) {
console.warn("ipc " + target + " " + method + " failed: " + error)
return { ran: true, output: "" }
}
return { ran: true, output: result === undefined || result === null ? "" : String(result) }
}
}
12 changes: 12 additions & 0 deletions shell/Commons/ShellIpc.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import QtQuick
import Quickshell.Io

// An IpcHandler the shell can also answer over its own socket (see
// IpcRegistry), so omarchy-shell reaches it without a qs ipc client. qs ipc
// still reaches it as before.
IpcHandler {
id: handler

Component.onCompleted: IpcRegistry.register(handler)
Component.onDestruction: IpcRegistry.unregister(handler)
}
2 changes: 2 additions & 0 deletions shell/Commons/qmldir
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module qs.Commons
singleton Border 1.0 Border.qml
singleton Color 1.0 Color.qml
singleton IpcRegistry 1.0 IpcRegistry.qml
singleton Style 1.0 Style.qml
singleton Util 1.0 Util.qml
ShellIpc 1.0 ShellIpc.qml
2 changes: 1 addition & 1 deletion shell/Ui/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Item {
id: panelController
}

IpcHandler {
ShellIpc {
enabled: root.manageIpc && root.ipcTarget !== ""
target: root.ipcTarget

Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/agents/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ Panel {
onTriggered: root.nowMs = Date.now()
}

IpcHandler {
ShellIpc {
target: root.ipcTarget
function open(): void { root.open() }
function close(): void { root.close() }
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/background/Background.qml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Item {
}
}

IpcHandler {
ShellIpc {
target: "background"

function refresh(): void {
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/bar/Bar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ Item {
// changes land in quick succession, stranding the bar off screen until the
// shell restarts. `omarchy-toggle-bar` nudges this after flipping the flag
// so the probe re-reads it even when the watch has gone quiet.
IpcHandler {
ShellIpc {
target: "omarchy.bar"

// Start rather than restart: a probe already in flight was launched by the
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/bar/widgets/Indicators.qml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ BarWidget {
? activeVerticalBlock.implicitHeight + inactiveVerticalArea.implicitHeight
: Math.max(activeHorizontalBlock.implicitHeight, inactiveHorizontalArea.implicitHeight)

IpcHandler {
ShellIpc {
target: "omarchy.indicators"

function refresh(): void {
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/bar/widgets/SystemUpdate.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ BarWidget {
implicitWidth: button.implicitWidth
implicitHeight: button.implicitHeight

IpcHandler {
ShellIpc {
target: "omarchy.system-update"

function refresh(): void {
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/lock/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ Item {
checkStrandedLock()
}

IpcHandler {
ShellIpc {
target: "lock"

function lock(): string {
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/notifications/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ Item {
return true
}

IpcHandler {
ShellIpc {
id: ipcHandler
target: "notifications"

Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/osd/Osd.qml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Item {
text: OsdModel.widestIcon
}

IpcHandler {
ShellIpc {
target: "osd"
function show(payloadJson: string): string {
root.open(payloadJson)
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/panels/bluetooth/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ Panel {
Quickshell.execDetached(["omarchy-bluetooth-power", adapter.enabled ? "off" : "on"])
}

IpcHandler {
ShellIpc {
target: "omarchy.bluetooth"

function open() { root.open() }
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/panels/clock/BarWidget.qml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ BarWidget {
}
}

IpcHandler {
ShellIpc {
target: "omarchy.clock"

function refresh(): void { root.broadcast("refresh") }
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/panels/dropbox/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Panel {
function onFilesChanged() { root.ensureCursor() }
}

IpcHandler {
ShellIpc {
target: root.ipcTarget
function open(): void { root.open() }
function close(): void { root.close() }
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/panels/monitor/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Panel {
})
}

IpcHandler {
ShellIpc {
target: "omarchy.monitor"

function brightness(percent: string): string { return root.brightnessIpc(percent) }
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/panels/network/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Panel {
Qt.callLater(function() { root.refresh(true) })
}

IpcHandler {
ShellIpc {
target: "omarchy.network"

function open() { root.open() }
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/panels/power/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Panel {
if (root.bar && root.bar.shell) root.bar.shell.updateEntryInline(root.moduleName, root.settings)
}

IpcHandler {
ShellIpc {
target: "omarchy.power"

function open() { root.open() }
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/panels/tailscale/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ Panel {
function onAccountsAccessDeniedChanged() { root.ensureCursor() }
}

IpcHandler {
ShellIpc {
target: root.ipcTarget
function open(): void { root.open() }
function close(): void { root.close() }
Expand Down
2 changes: 1 addition & 1 deletion shell/plugins/panels/weather/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ Panel {
onTriggered: root.refresh()
}

IpcHandler {
ShellIpc {
target: root.ipcTarget

function open(): void { root.openFromHotkey() }
Expand Down
3 changes: 2 additions & 1 deletion shell/plugins/services/idle/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Quickshell.Hyprland
import Quickshell.Io
import Quickshell.Wayland
import "IdleModel.js" as IdleModel
import qs.Commons

Item {
id: root
Expand Down Expand Up @@ -335,7 +336,7 @@ Item {
refreshStayAwakeState()
}

IpcHandler {
ShellIpc {
target: "idle"

function status(): string {
Expand Down
3 changes: 2 additions & 1 deletion shell/plugins/services/media/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Quickshell.Io
import Quickshell.Services.Mpris
import Quickshell.Services.Pipewire
import "MediaModel.js" as MediaModel
import qs.Commons

Item {
id: root
Expand Down Expand Up @@ -530,7 +531,7 @@ Item {
return true
}

IpcHandler {
ShellIpc {
id: ipcHandler
target: "media"

Expand Down
3 changes: 2 additions & 1 deletion shell/plugins/services/nightlight/Service.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import QtQuick
import Quickshell.Io
import "NightlightModel.js" as NightlightModel
import qs.Commons

Item {
id: root
Expand Down Expand Up @@ -85,7 +86,7 @@ Item {

Component.onCompleted: refresh()

IpcHandler {
ShellIpc {
target: "nightlight"

function status(): string {
Expand Down
Loading