Skip to content

Instantly share code, notes, and snippets.

@anosatsuk124
Last active August 31, 2025 07:17
Show Gist options
  • Save anosatsuk124/7751287eb245972d6115ea3cf31c5b72 to your computer and use it in GitHub Desktop.
Save anosatsuk124/7751287eb245972d6115ea3cf31c5b72 to your computer and use it in GitHub Desktop.
[Unit]
Description=iPad Sunshine Service for configuration: %i
[Service]
Type=simple
ExecStart=/bin/sh -c '/usr/bin/sunshine output_name=%i'
#!/usr/bin/env bash
set -euo pipefail
# Wayland compositor detection
detect_compositor() {
if command -v hyprctl >/dev/null 2>&1 && hyprctl version >/dev/null 2>&1; then
echo "hyprland"
elif command -v swaymsg >/dev/null 2>&1 && swaymsg -t get_version >/dev/null 2>&1; then
echo "sway"
else
echo "unknown"
fi
}
# Common settings
width=$((2360 / 1))
height=$((1640 / 1))
scale=2
compositor=$(detect_compositor)
case "$compositor" in
"sway")
# プライマリ出力の幅を取得
primary_height=$(swaymsg -t get_outputs -r | jq -r '.[] | select(.rect.x == 0 and .rect.y == 0) | select(.current_mode != null) | .current_mode.height')
primary_width=$(swaymsg -t get_outputs -r | jq -r '.[] | select(.rect.x == 0 and .rect.y == 0) | select(.current_mode != null) | .current_mode.width')
# 既存の HEADLESS-* 出力名を探す
output_name=$(swaymsg -t get_outputs -r |
jq -r '.[] | select(.name | test("^HEADLESS-")) | .name')
output_is_active=$(swaymsg -t get_outputs -r |
jq -r '.[] | select(.name | test("^HEADLESS-")) | .active')
if [ "$output_is_active" = "true" ]; then
swaymsg output "$output_name" unplug
systemctl --user stop ipad-wayvnc.service
systemctl --user stop 'ipad-sunshine@*.service'
notify-send "iPad output disabled"
exit 0
elif [ -z "$output_name" ]; then
swaymsg create_output
output_name=$(swaymsg -t get_outputs -r | jq -r '.[] | select(.name | test("^HEADLESS-")) | .name')
swaymsg output "$output_name" mode ${height}x${width}@60Hz scale ${scale}
fi
swaymsg output "$output_name" enable
is_exist_DP3=$(swaymsg -t get_outputs -r | jq -r '.[] | select(.name == "DP-3") | .name')
is_exist_eDP_1=$(swaymsg -t get_outputs -r | jq -r '.[] | select(.name == "eDP-1") | .name')
headless_number=$(swaymsg -t get_outputs -r | jq -r 'to_entries[] | select(.value.name | test("^HEADLESS-")) | .key')
if [ -n "$is_exist_DP3" ]; then
wlr-randr --output "$output_name" --scale ${scale} --mode ${width}x${height}@60Hz --below DP-3
elif [ -n "$is_exist_eDP_1" ]; then
wlr-randr --output "$output_name" --scale ${scale} --mode ${width}x${height}@60Hz --right-of eDP-1
fi
;;
"hyprland")
# 既存の headless-* 出力名を探す
output_name=$(hyprctl monitors -j | jq -r '.[] | select(.name | test("^headless-")) | .name')
output_is_active=$(hyprctl monitors -j | jq -r '.[] | select(.name | test("^headless-")) | .disabled | not')
if [ "$output_is_active" = "true" ] && [ -n "$output_name" ]; then
hyprctl output remove "$output_name"
systemctl --user stop ipad-wayvnc.service
systemctl --user stop 'ipad-sunshine@*.service'
notify-send "iPad output disabled"
exit 0
fi
# 仮想モニターを作成または有効化
if [ -z "$output_name" ]; then
# Hyprlandでは動的に仮想モニターを作成
output_name="headless-1"
hyprctl output create headless "${output_name}"
hyprctl keyword monitor "${output_name},${width}x${height}@60,auto,${scale}"
else
hyprctl keyword monitor "$output_name,${width}x${height}@60,auto,${scale}"
fi
is_exist_DP3=$(hyprctl monitors -j | jq -r '.[] | select(.name == "DP-3") | .name')
is_exist_eDP_1=$(hyprctl monitors -j | jq -r '.[] | select(.name == "eDP-1") | .name')
if [ -n "$is_exist_DP3" ]; then
hyprctl keyword monitor "DP-3,preferred,auto-right,1"
hyprctl keyword monitor "$output_name,${width}x${height}@60,auto-down,${scale}"
elif [ -n "$is_exist_eDP_1" ]; then
hyprctl keyword monitor "$output_name,${width}x${height}@60,auto-right,${scale}"
fi
# Hyprlandでは番号を別の方法で取得
headless_number=$(hyprctl monitors -j | jq -r 'to_entries[] | select(.value.name | test("^headless-")) | .key')
;;
*)
echo "Error: Unsupported compositor. Only Sway and Hyprland are supported." >&2
exit 1
;;
esac
systemctl --user restart ipad-wayvnc.service
systemctl --user start "ipad-sunshine@${headless_number}.service"
# sudo sh -c 'systemctl stop rustdesk.service && systemctl start rustdesk.service'
notify-send "iPad output enabled at ${width}x${height}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment