1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/fs"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// Sizing a window before recording it
// ============================================================================
//
// An upright recording is a window of that shape. A tiled window alone in its
// column already has the full height of the working area, so only its width
// needs to change: niri sets column widths in pixels, the window stays in its
// row, and nothing floats. A column shared with other windows has less
// height; the width is computed from the window's own height, so the shape is
// still right, only smaller.
//
// The width before is saved, once, in resize-saved.json, and put back by
// `resize --aspect=restore`, which `run` does after every recording of that
// window, however it ended. A second resize of the same window (the panel
// trying 9:16, then 4:5) keeps the first saved width, so restoring always
// returns to how it was before any of them.
type windowLayout struct {
PosInScrollingLayout []int `json:"pos_in_scrolling_layout"`
TileSize []float64 `json:"tile_size"`
WindowSize []int `json:"window_size"`
}
type windowInfo struct {
ID uint64 `json:"id"`
AppID string `json:"app_id"`
IsFloating bool `json:"is_floating"`
Layout windowLayout `json:"layout"`
}
func windowByID(id uint64) (windowInfo, error) {
var ws []windowInfo
if err := niriJSON(&ws, "windows"); err != nil {
return windowInfo{}, err
}
for _, w := range ws {
if w.ID == id {
return w, nil
}
}
return windowInfo{}, fmt.Errorf("niri has no window %d", id)
}
// saved is the window's size before the first resize.
type saved struct {
Window uint64 `json:"window"`
Width int `json:"width"`
Height int `json:"height"`
Floating bool `json:"floating"`
// The shape last set, for the recording's label.
Aspect string `json:"aspect,omitempty"`
}
func savedPath() string { return filepath.Join(runtimeDir(), "resize-saved.json") }
func readSaved() (saved, bool) {
b, err := os.ReadFile(savedPath())
if err != nil {
return saved{}, false
}
var s saved
if json.Unmarshal(b, &s) != nil || s.Window == 0 {
return saved{}, false
}
return s, true
}
func writeSaved(s saved) error {
if err := os.MkdirAll(runtimeDir(), 0o700); err != nil {
return err
}
b, err := json.Marshal(s)
if err != nil {
return err
}
return os.WriteFile(savedPath(), append(b, '\n'), 0o600)
}
func parseAspect(a string) (w, h float64, err error) {
x, y, ok := strings.Cut(a, ":")
if ok {
w, err1 := strconv.ParseFloat(x, 64)
h, err2 := strconv.ParseFloat(y, 64)
if err1 == nil && err2 == nil && w > 0 && h > 0 {
return w, h, nil
}
}
return 0, 0, fmt.Errorf("--aspect=%s: want W:H (e.g. 9:16), or restore", a)
}
// even rounds down to an even number: the H.264 encoder halves chroma both
// ways, so odd sizes would be cut by a pixel anyway.
func even(v float64) int { return int(math.Floor(v/2)) * 2 }
func resizeCmd(args []string) error {
fs := flag.NewFlagSet("resize", flag.ExitOnError)
id := fs.Uint64("window", 0, "the niri window id")
aspect := fs.String("aspect", "", "W:H, or restore")
_ = fs.Parse(args)
if *id == 0 || *aspect == "" || fs.NArg() != 0 {
return errors.New("usage: screenrecord resize --window=ID --aspect=W:H|restore")
}
if *aspect == "restore" {
return restoreWindow(*id)
}
if err := shapeWindow(*id, *aspect); err != nil {
return err
}
// Bring it into view, so the new shape can be seen.
_ = niriAction("focus-window", "--id", strconv.FormatUint(*id, 10))
return nil
}
// shapeWindow gives a tiled window the shape W:H by its column's width,
// saving the width before (once) for restoreWindow.
func shapeWindow(id uint64, aspect string) error {
aw, ah, err := parseAspect(aspect)
if err != nil {
return err
}
w, err := windowByID(id)
if err != nil {
return err
}
if w.IsFloating {
return errors.New("the window floats; shaping is for tiled windows")
}
if len(w.Layout.WindowSize) != 2 || len(w.Layout.TileSize) != 2 {
return errors.New("niri reports no size for the window")
}
s, ok := readSaved()
if !ok || s.Window != id {
s = saved{Window: id, Width: int(math.Round(w.Layout.TileSize[0])),
Height: w.Layout.WindowSize[1]}
}
s.Aspect = aspect
if err := writeSaved(s); err != nil {
return err
}
// The height is the column's; the width follows. The tile includes the
// border around the window, so the difference is added back.
height := float64(even(float64(w.Layout.WindowSize[1])))
border := w.Layout.TileSize[0] - float64(w.Layout.WindowSize[0])
width := float64(even(height*aw/ah)) + border
return setWidth(id, int(width))
}
// setWidth sets the window's column to a fixed width in logical pixels
// (niri's SizeChange SetFixed: a bare number).
func setWidth(id uint64, width int) error {
return niriAction("set-window-width", "--id", strconv.FormatUint(id, 10), strconv.Itoa(width))
}
func restoreWindow(id uint64) error {
s, ok := readSaved()
if !ok || s.Window != id {
return nil // never resized, or already restored
}
if _, err := windowByID(id); err == nil {
if err := setWidth(id, s.Width); err != nil {
return err
}
}
if err := os.Remove(savedPath()); err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
return nil
}
// restoreAfterRun is what run does after a recording of a window: put back
// a width the panel changed. Errors are logged, not returned; the recording
// itself is finished either way.
func restoreAfterRun(id uint64) {
if id == 0 {
return
}
// A window that closed during the recording takes niri a moment to
// drop from its list; restoring a gone window is a no-op anyway.
for i := 0; i < 3; i++ {
if err := restoreWindow(id); err == nil {
return
} else if i == 2 {
fmt.Fprintln(os.Stderr, "screenrecord: restoring the window's width:", err)
}
time.Sleep(200 * time.Millisecond)
}
}
|