| @@ -7,6 +7,7 @@ import ( |
| 7 | 7 | "errors" |
| 8 | 8 | "fmt" |
| 9 | 9 | "net/http" |
| 10 | +新增: "strconv" |
| 10 | 11 | "strings" |
| 11 | 12 | "time" |
| 12 | 13 | |
| @@ -195,28 +196,38 @@ systemctl is-active --quiet cage-claude-desktop.service |
| 195 | 196 | password=$(cat /home/cage/.config/claude-cage/desktop-password) |
| 196 | 197 | printf '%s\n' "$password" |
| 197 | 198 | if test -f /run/cage-desktop/base-display-scale; then cat /run/cage-desktop/base-display-scale |
| 198 | | −删除:elif test -f /run/cage-desktop/display-scale; then cat /run/cage-desktop/display-scale; fi`) |
| 199 | +新增:elif test -f /run/cage-desktop/display-scale; then cat /run/cage-desktop/display-scale |
| 200 | +新增:else echo; fi |
| 201 | +新增:if test -f /run/cage-desktop/xsettings.pid; then echo dynamic; fi`) |
| 199 | 202 | password, scale, _ := strings.Cut(strings.TrimSpace(string(b)), "\n") |
| 200 | 203 | if err != nil || len(password) != 8 { |
| 201 | 204 | http.Error(w, "Claude Desktop 尚未启动", 409) |
| 202 | 205 | return |
| 203 | 206 | } |
| 207 | +新增: // With a settings manager the viewer chooses the density of its own screen. |
| 208 | +新增: scale, dynamic, _ := strings.Cut(scale, "\n") |
| 204 | 209 | density := 2.0 |
| 205 | 210 | if scale == "1.5" { |
| 206 | 211 | density = 1.5 |
| 207 | 212 | } |
| 208 | | −删除: jsonReply(w, map[string]any{"password": password, "display_scale": density}) |
| 213 | +新增: jsonReply(w, map[string]any{"password": password, "display_scale": density, "dynamic_scale": dynamic == "dynamic"}) |
| 214 | +新增:} |
| 215 | +新增: |
| 216 | +新增:// The zero value is a viewer that leaves the display's density alone. |
| 217 | +新增:func validDisplayDensity(scale float64) bool { |
| 218 | +新增: return scale == 0 || scale >= 0.75 && scale <= 4 |
| 209 | 219 | } |
| 210 | 220 | |
| 211 | 221 | func (s *Server) claudeDesktopResize(w http.ResponseWriter, r *http.Request) { |
| 212 | 222 | var input struct { |
| 213 | | −删除: Width int `json:"width"` |
| 214 | | −删除: Height int `json:"height"` |
| 223 | +新增: Width int `json:"width"` |
| 224 | +新增: Height int `json:"height"` |
| 225 | +新增: Scale float64 `json:"scale"` |
| 215 | 226 | } |
| 216 | 227 | if !readJSON(w, r, &input) { |
| 217 | 228 | return |
| 218 | 229 | } |
| 219 | | −删除: if input.Width < 320 || input.Height < 240 || input.Width > 8192 || input.Height > 8192 { |
| 230 | +新增: if input.Width < 320 || input.Height < 240 || input.Width > 8192 || input.Height > 8192 || !validDisplayDensity(input.Scale) { |
| 220 | 231 | http.Error(w, "画面尺寸无效", 400) |
| 221 | 232 | return |
| 222 | 233 | } |
| @@ -230,14 +241,55 @@ func (s *Server) claudeDesktopResize(w http.ResponseWriter, r *http.Request) { |
| 230 | 241 | jsonReply(w, map[string]bool{"ok": true}) |
| 231 | 242 | return |
| 232 | 243 | } |
| 233 | | −删除: // Fixed commands and validated integers only; no host shell executes input. |
| 234 | | −删除: wid, hei := input.Width, input.Height |
| 235 | | −删除: script := fmt.Sprintf(`set -eu |
| 244 | +新增: scale := "" |
| 245 | +新增: if input.Scale != 0 { |
| 246 | +新增: scale = strconv.FormatFloat(input.Scale, 'f', -1, 64) |
| 247 | +新增: } |
| 248 | +新增: script := desktopResizeScript(input.Width, input.Height, scale) |
| 249 | +新增: ctx, cancel := context.WithTimeout(r.Context(), 8*time.Second) |
| 250 | +新增: defer cancel() |
| 251 | +新增: output, err := s.guestQuick(ctx, script) |
| 252 | +新增: if err != nil { |
| 253 | +新增: http.Error(w, "Claude Desktop 画面尺寸未能调整", 502) |
| 254 | +新增: return |
| 255 | +新增: } |
| 256 | +新增: reply := map[string]any{"ok": true} |
| 257 | +新增: if applied, _, _ := strings.Cut(string(output), "\n"); scale != "" && strings.TrimSpace(applied) == "scale "+scale { |
| 258 | +新增: reply["scale"] = input.Scale |
| 259 | +新增: } |
| 260 | +新增: jsonReply(w, reply) |
| 261 | +新增:} |
| 262 | +新增: |
| 263 | +新增:// Fixed commands and validated numbers only; no host shell executes input. |
| 264 | +新增:// scale is empty for a viewer that leaves the display's density alone. |
| 265 | +新增:func desktopResizeScript(wid, hei int, scale string) string { |
| 266 | +新增: return fmt.Sprintf(`set -eu |
| 236 | 267 | export DISPLAY=:78 XAUTHORITY=/run/cage-desktop/Xauthority |
| 237 | 268 | exec 9>/run/cage-desktop/resize.lock |
| 238 | 269 | flock 9 |
| 239 | | −删除:printf '%%s %%s\n' %d %d > /run/cage-desktop/desktop-size |
| 270 | +新增:requested=%s |
| 271 | +新增:manager= |
| 272 | +新增:if test -r /run/cage-desktop/xsettings.pid; then |
| 273 | +新增: manager=$(cat /run/cage-desktop/xsettings.pid) |
| 274 | +新增: if test "$(cat "/proc/$manager/comm" 2>/dev/null)" != xsettingsd; then manager=; fi |
| 275 | +新增:fi |
| 276 | +新增:# A fixed-density session ignores the request, and its viewer sizes for that |
| 277 | +新增:# density. A viewer that names none sized its display for the configured one. |
| 278 | +新增:scale= |
| 279 | +新增:if test -n "$manager"; then |
| 280 | +新增: scale=${requested:-$(cat /run/cage-desktop/base-display-scale 2>/dev/null || echo 2)} |
| 281 | +新增: if test -n "$requested"; then echo "scale $scale"; fi |
| 282 | +新增:fi |
| 283 | +新增:printf '%%s %%s %%s\n' %d %d "$scale" > /run/cage-desktop/desktop-size |
| 284 | +新增:# A phone holds the display. Its release applies this size and density. |
| 240 | 285 | if test -f /run/cage-phone-desktop/layout.json; then exit 0; fi |
| 286 | +新增:if test -n "$manager" && test "$(cat /run/cage-desktop/display-scale 2>/dev/null)" != "$scale"; then |
| 287 | +新增: dpi=$(awk -v scale="$scale" 'BEGIN {printf "%%.0f", scale * 96 * 1024}') |
| 288 | +新增: printf 'Xft/DPI %%s\nGdk/WindowScalingFactor 1\nGdk/UnscaledDPI %%s\nGtk/DecorationLayout ":"\n' "$dpi" "$dpi" > /run/cage-desktop/xsettings.conf.new |
| 289 | +新增: mv /run/cage-desktop/xsettings.conf.new /run/cage-desktop/xsettings.conf |
| 290 | +新增: kill -HUP "$manager" |
| 291 | +新增: printf '%%s\n' "$scale" > /run/cage-desktop/display-scale |
| 292 | +新增:fi |
| 241 | 293 | name=cage-%dx%d |
| 242 | 294 | if ! xrandr --query | grep -q " $name "; then |
| 243 | 295 | xrandr --newmode "$name" %.3f %d %d %d %d %d %d %d %d +HSync -VSync |
| @@ -246,14 +298,7 @@ fi |
| 246 | 298 | xrandr --output VNC-0 --mode "$name" |
| 247 | 299 | for old in $(xrandr --query | awk '/^ +cage-/ {print $1}'); do |
| 248 | 300 | if [ "$old" != "$name" ]; then xrandr --delmode VNC-0 "$old"; xrandr --rmmode "$old"; fi |
| 249 | | −删除:done`, wid, hei, wid, hei, float64((wid+32)*(hei+8)*60)/1000000, wid, wid+8, wid+16, wid+32, hei, hei+1, hei+2, hei+8) |
| 250 | | −删除: ctx, cancel := context.WithTimeout(r.Context(), 8*time.Second) |
| 251 | | −删除: defer cancel() |
| 252 | | −删除: if _, err := s.guestQuick(ctx, script); err != nil { |
| 253 | | −删除: http.Error(w, "Claude Desktop 画面尺寸未能调整", 502) |
| 254 | | −删除: return |
| 255 | | −删除: } |
| 256 | | −删除: jsonReply(w, map[string]bool{"ok": true}) |
| 301 | +新增:done`, scale, wid, hei, wid, hei, float64((wid+32)*(hei+8)*60)/1000000, wid, wid+8, wid+16, wid+32, hei, hei+1, hei+2, hei+8) |
| 257 | 302 | } |
| 258 | 303 | |
| 259 | 304 | func (s *Server) claudeDesktopVNC(w http.ResponseWriter, r *http.Request) { |