forked from kendrick90/SpoutCam
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspoutcam_settings.ps1
More file actions
365 lines (331 loc) · 14.9 KB
/
Copy pathspoutcam_settings.ps1
File metadata and controls
365 lines (331 loc) · 14.9 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# ============================================================================
# SpoutCam Settings — unified GUI for mapping Spout senders to SpoutCam1..16.
#
# - Enumerates only the SpoutCam variants that are actually REGISTERED
# (HKLM\SOFTWARE\Classes\CLSID\{8E14549A-DB61-4309-AFA1-3578E927E933..42}).
# - Reads currently active Spout senders from the shared-memory file
# "SpoutSenderNames" (64 slots × 256 bytes by default; configurable via
# HKCU\Software\Leading Edge\Spout\MaxSenders).
# - Sender column is a ComboBox: pick from active senders OR type any name
# (so you can pre-bind to a sender that isn't running yet).
# - Settings are written per-camera at HKCU\Software\Leading Edge\SpoutCamN.
# This requires the SPOUTCAM_REGISTRY_PATH macro in cam.h. Older builds all
# shared HKCU\Software\Leading Edge\SpoutCam.
#
# Settings are picked up by SpoutCam at startup. Apps already holding a camera
# open must close and reopen it for changes to take effect.
# ============================================================================
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$RegistryRoot = 'HKCU:\Software\Leading Edge'
$ClsidBase = '8E14549A-DB61-4309-AFA1-3578E927E9' # last byte 0x33..0x42 → SpoutCam1..16
$FpsOptions = @('10','15','25','30','50','60')
$FpsDefault = 3 # 30 fps
$ResOptions = @(
'Active Sender',
'320 x 240', '640 x 360', '640 x 480', '800 x 600',
'1024 x 720', '1024 x 768',
'1280 x 720', '1280 x 960', '1280 x 1024', '1920 x 1080'
)
$ResDefault = 0
# ---------- Discovery ----------
function Get-RegisteredSpoutCams {
# Returns an array of registered cam numbers (1..16) by checking which CLSIDs exist in HKLM.
$registered = @()
foreach ($xx in 0x33..0x42) {
$g = "{{$ClsidBase{0:X2}}}" -f $xx
$p = Join-Path 'HKLM:\SOFTWARE\Classes\CLSID' $g
if (Test-Path $p) {
$registered += ($xx - 0x32) # 0x33 → cam 1, 0x34 → cam 2, …
}
}
return ,$registered
}
function Get-ActiveSpoutSenders {
# Reads the SpoutSenderNames shared-memory file and returns active sender names.
# Format: N slots × 256 bytes, each null-terminated ASCII. Empty slot starts with 0x00.
$names = @()
$mmf = $null
try {
$mmf = [System.IO.MemoryMappedFiles.MemoryMappedFile]::OpenExisting('SpoutSenderNames')
} catch {
return ,$names # MMF doesn't exist → no Spout app running
}
try {
$acc = $mmf.CreateViewAccessor(0, 0, [System.IO.MemoryMappedFiles.MemoryMappedFileAccess]::Read)
try {
$cap = [int]$acc.Capacity
$slotSize = 256
$slots = [Math]::Floor($cap / $slotSize)
for ($i = 0; $i -lt $slots; $i++) {
$offset = $i * $slotSize
$buf = New-Object byte[] $slotSize
$null = $acc.ReadArray($offset, $buf, 0, $slotSize)
# Find first null
$end = 0
while ($end -lt $slotSize -and $buf[$end] -ne 0) { $end++ }
if ($end -gt 0) {
$name = [System.Text.Encoding]::ASCII.GetString($buf, 0, $end)
if ($name.Trim().Length -gt 0) { $names += $name }
}
}
} finally {
$acc.Dispose()
}
} finally {
$mmf.Dispose()
}
return ,(@($names | Sort-Object -Unique))
}
function Get-CamSettings {
param([int]$Index)
$key = Join-Path $RegistryRoot "SpoutCam$Index"
$obj = [pscustomobject]@{
Cam = "SpoutCam$Index"
Sender = ''
FpsIndex = $FpsDefault
ResIndex = $ResDefault
Mirror = $false
Swap = $false
Flip = $false
}
if (Test-Path $key) {
$p = Get-ItemProperty $key -ErrorAction SilentlyContinue
if ($p.senderstart -ne $null) { $obj.Sender = [string]$p.senderstart }
if ($p.fps -ne $null) { $obj.FpsIndex = [int]$p.fps }
if ($p.resolution -ne $null) { $obj.ResIndex = [int]$p.resolution }
if ($p.mirror -ne $null) { $obj.Mirror = ([int]$p.mirror) -ne 0 }
if ($p.swap -ne $null) { $obj.Swap = ([int]$p.swap) -ne 0 }
if ($p.flip -ne $null) { $obj.Flip = ([int]$p.flip) -ne 0 }
}
if ($obj.FpsIndex -lt 0 -or $obj.FpsIndex -ge $FpsOptions.Count) { $obj.FpsIndex = $FpsDefault }
if ($obj.ResIndex -lt 0 -or $obj.ResIndex -ge $ResOptions.Count) { $obj.ResIndex = $ResDefault }
return $obj
}
function Set-CamSettings {
param([int]$Index, [string]$Sender, [int]$FpsIndex, [int]$ResIndex, [bool]$Mirror, [bool]$Swap, [bool]$Flip)
$key = Join-Path $RegistryRoot "SpoutCam$Index"
if (-not (Test-Path $key)) { New-Item -Path $key -Force | Out-Null }
Set-ItemProperty -Path $key -Name 'senderstart' -Value $Sender -Type String
Set-ItemProperty -Path $key -Name 'fps' -Value $FpsIndex -Type DWord
Set-ItemProperty -Path $key -Name 'resolution' -Value $ResIndex -Type DWord
Set-ItemProperty -Path $key -Name 'mirror' -Value ([int]$Mirror) -Type DWord
Set-ItemProperty -Path $key -Name 'swap' -Value ([int]$Swap) -Type DWord
Set-ItemProperty -Path $key -Name 'flip' -Value ([int]$Flip) -Type DWord
}
function Reset-CamSettings {
param([int]$Index)
$key = Join-Path $RegistryRoot "SpoutCam$Index"
if (Test-Path $key) { Remove-Item -Path $key -Recurse -Force -ErrorAction SilentlyContinue }
}
# ---------- Form ----------
$form = New-Object System.Windows.Forms.Form
$form.Text = 'SpoutCam Settings'
$form.Size = New-Object System.Drawing.Size(960, 640)
$form.MinimumSize = New-Object System.Drawing.Size(700, 400)
$form.StartPosition = 'CenterScreen'
$lbl = New-Object System.Windows.Forms.Label
$lbl.Location = New-Object System.Drawing.Point(12, 8)
$lbl.AutoSize = $true
$lbl.Text = 'Per-camera settings. Sender column suggests active Spout senders; you can also type any name.'
$form.Controls.Add($lbl)
$senderStatus = New-Object System.Windows.Forms.Label
$senderStatus.Location = New-Object System.Drawing.Point(12, 28)
$senderStatus.AutoSize = $true
$senderStatus.ForeColor = [System.Drawing.Color]::DarkSlateGray
$senderStatus.Text = ''
$form.Controls.Add($senderStatus)
$grid = New-Object System.Windows.Forms.DataGridView
$grid.Location = New-Object System.Drawing.Point(12, 52)
$grid.Size = New-Object System.Drawing.Size(920, 490)
$grid.Anchor = 'Top,Left,Right,Bottom'
$grid.RowHeadersVisible = $false
$grid.AllowUserToAddRows = $false
$grid.AllowUserToDeleteRows = $false
$grid.AllowUserToResizeRows = $false
$grid.AutoSizeColumnsMode = 'None'
$grid.SelectionMode = 'CellSelect'
$grid.EditMode = 'EditOnEnter'
$grid.AlternatingRowsDefaultCellStyle.BackColor = [System.Drawing.Color]::FromArgb(245, 245, 250)
# Suppress data errors on ComboBoxColumn when user types a value not in the list.
$grid.add_DataError({
param($s, $e)
$e.ThrowException = $false
$e.Cancel = $false
})
# Columns
$colCam = New-Object System.Windows.Forms.DataGridViewTextBoxColumn
$colCam.HeaderText = 'Camera'; $colCam.Name = 'Cam'; $colCam.ReadOnly = $true; $colCam.Width = 90
$null = $grid.Columns.Add($colCam)
$colSender = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
$colSender.HeaderText = 'Spout Sender'; $colSender.Name = 'Sender'; $colSender.Width = 320
$colSender.FlatStyle = 'Flat'
# Allow values not in the dropdown to display
$colSender.DisplayStyleForCurrentCellOnly = $true
$null = $grid.Columns.Add($colSender)
$colFps = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
$colFps.HeaderText = 'FPS'; $colFps.Name = 'Fps'; $colFps.Width = 70; $colFps.FlatStyle = 'Flat'
foreach ($v in $FpsOptions) { $null = $colFps.Items.Add($v) }
$null = $grid.Columns.Add($colFps)
$colRes = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
$colRes.HeaderText = 'Resolution'; $colRes.Name = 'Res'; $colRes.Width = 130; $colRes.FlatStyle = 'Flat'
foreach ($v in $ResOptions) { $null = $colRes.Items.Add($v) }
$null = $grid.Columns.Add($colRes)
$colMirror = New-Object System.Windows.Forms.DataGridViewCheckBoxColumn
$colMirror.HeaderText = 'Mirror'; $colMirror.Name = 'Mirror'; $colMirror.Width = 60
$null = $grid.Columns.Add($colMirror)
$colSwap = New-Object System.Windows.Forms.DataGridViewCheckBoxColumn
$colSwap.HeaderText = 'Swap RGB/BGR'; $colSwap.Name = 'Swap'; $colSwap.Width = 110
$null = $grid.Columns.Add($colSwap)
$colFlip = New-Object System.Windows.Forms.DataGridViewCheckBoxColumn
$colFlip.HeaderText = 'Flip'; $colFlip.Name = 'Flip'; $colFlip.Width = 50
$null = $grid.Columns.Add($colFlip)
# Make Sender ComboBox editable (allow typing names not in the dropdown).
$grid.add_EditingControlShowing({
param($s, $e)
if ($grid.CurrentCell -and $grid.CurrentCell.OwningColumn.Name -eq 'Sender' -and ($e.Control -is [System.Windows.Forms.ComboBox])) {
$cb = $e.Control
$cb.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDown
$cb.AutoCompleteMode = [System.Windows.Forms.AutoCompleteMode]::SuggestAppend
$cb.AutoCompleteSource = [System.Windows.Forms.AutoCompleteSource]::ListItems
}
})
# When the Sender combobox loses focus / commits, push the typed value back into the cell.
$grid.add_CellLeave({
param($s, $e)
if ($e.ColumnIndex -eq $colSender.Index) {
$editCtrl = $grid.EditingControl
if ($editCtrl -is [System.Windows.Forms.ComboBox]) {
$grid.Rows[$e.RowIndex].Cells[$e.ColumnIndex].Value = $editCtrl.Text
}
}
})
function Refresh-SenderList {
$colSender.Items.Clear()
$active = Get-ActiveSpoutSenders
foreach ($n in $active) { $null = $colSender.Items.Add($n) }
if ($active.Count -gt 0) {
$senderStatus.Text = ('Active Spout senders found: {0} ({1})' -f $active.Count, ($active -join ', '))
$senderStatus.ForeColor = [System.Drawing.Color]::DarkGreen
} else {
$senderStatus.Text = 'No active Spout senders detected. You can still type sender names manually.'
$senderStatus.ForeColor = [System.Drawing.Color]::DarkSlateGray
}
}
function Load-Grid {
$grid.Rows.Clear()
$registered = Get-RegisteredSpoutCams
if ($registered.Count -eq 0) {
$form.Text = 'SpoutCam Settings — NO REGISTERED CAMERAS'
return
}
$form.Text = ("SpoutCam Settings — {0} registered" -f $registered.Count)
foreach ($i in $registered) {
$s = Get-CamSettings -Index $i
# Pre-add the saved sender so it's selectable even if not currently active.
if ($s.Sender -and -not $colSender.Items.Contains($s.Sender)) {
$null = $colSender.Items.Add($s.Sender)
}
$null = $grid.Rows.Add(
$s.Cam,
$s.Sender,
$FpsOptions[$s.FpsIndex],
$ResOptions[$s.ResIndex],
$s.Mirror,
$s.Swap,
$s.Flip
)
}
}
# ---------- Buttons ----------
$btnSave = New-Object System.Windows.Forms.Button
$btnSave.Text = 'Save All'
$btnSave.Location = New-Object System.Drawing.Point(12, 555)
$btnSave.Size = New-Object System.Drawing.Size(100, 32)
$btnSave.Anchor = 'Bottom,Left'
$btnSave.Add_Click({
try {
$grid.EndEdit()
$count = 0
foreach ($row in $grid.Rows) {
$camLabel = [string]$row.Cells['Cam'].Value # e.g. "SpoutCam5"
$camNum = [int]($camLabel -replace '\D','')
$sender = "$($row.Cells['Sender'].Value)"
$fpsIdx = [Math]::Max(0, $FpsOptions.IndexOf("$($row.Cells['Fps'].Value)"))
$resIdx = [Math]::Max(0, $ResOptions.IndexOf("$($row.Cells['Res'].Value)"))
$mirror = [bool]$row.Cells['Mirror'].Value
$swap = [bool]$row.Cells['Swap'].Value
$flip = [bool]$row.Cells['Flip'].Value
Set-CamSettings -Index $camNum -Sender $sender -FpsIndex $fpsIdx -ResIndex $resIdx -Mirror $mirror -Swap $swap -Flip $flip
$count++
}
[void][System.Windows.Forms.MessageBox]::Show("Saved settings for $count camera(s).`r`n`r`nApps already using a camera must close and reopen it.", 'Saved', 'OK', 'Information')
} catch {
[void][System.Windows.Forms.MessageBox]::Show("Save failed: $($_.Exception.Message)", 'Error', 'OK', 'Error')
}
})
$form.Controls.Add($btnSave)
$btnReload = New-Object System.Windows.Forms.Button
$btnReload.Text = 'Reload'
$btnReload.Location = New-Object System.Drawing.Point(122, 555)
$btnReload.Size = New-Object System.Drawing.Size(100, 32)
$btnReload.Anchor = 'Bottom,Left'
$btnReload.Add_Click({ Refresh-SenderList; Load-Grid })
$form.Controls.Add($btnReload)
$btnRefreshSenders = New-Object System.Windows.Forms.Button
$btnRefreshSenders.Text = 'Refresh Senders'
$btnRefreshSenders.Location = New-Object System.Drawing.Point(232, 555)
$btnRefreshSenders.Size = New-Object System.Drawing.Size(140, 32)
$btnRefreshSenders.Anchor = 'Bottom,Left'
$btnRefreshSenders.Add_Click({ Refresh-SenderList })
$form.Controls.Add($btnRefreshSenders)
$btnReset = New-Object System.Windows.Forms.Button
$btnReset.Text = 'Reset Selected'
$btnReset.Location = New-Object System.Drawing.Point(382, 555)
$btnReset.Size = New-Object System.Drawing.Size(120, 32)
$btnReset.Anchor = 'Bottom,Left'
$btnReset.Add_Click({
$sel = @{}
foreach ($cell in $grid.SelectedCells) { $sel[$cell.RowIndex] = $true }
foreach ($row in $grid.SelectedRows) { $sel[$row.Index] = $true }
if ($sel.Count -eq 0) {
[void][System.Windows.Forms.MessageBox]::Show('Select one or more rows first.', 'Reset', 'OK', 'Information')
return
}
$confirm = [System.Windows.Forms.MessageBox]::Show("Reset $($sel.Count) camera(s) to defaults? This deletes their registry subkey.", 'Confirm', 'YesNo', 'Question')
if ($confirm -ne 'Yes') { return }
foreach ($idx in $sel.Keys) {
$camLabel = [string]$grid.Rows[$idx].Cells['Cam'].Value
$camNum = [int]($camLabel -replace '\D','')
Reset-CamSettings -Index $camNum
}
Load-Grid
})
$form.Controls.Add($btnReset)
$btnApplyAll = New-Object System.Windows.Forms.Button
$btnApplyAll.Text = 'Set All FPS/Res to Row 1'
$btnApplyAll.Location = New-Object System.Drawing.Point(512, 555)
$btnApplyAll.Size = New-Object System.Drawing.Size(170, 32)
$btnApplyAll.Anchor = 'Bottom,Left'
$btnApplyAll.Add_Click({
$grid.EndEdit()
if ($grid.Rows.Count -eq 0) { return }
$fps = $grid.Rows[0].Cells['Fps'].Value
$res = $grid.Rows[0].Cells['Res'].Value
for ($i = 1; $i -lt $grid.Rows.Count; $i++) {
$grid.Rows[$i].Cells['Fps'].Value = $fps
$grid.Rows[$i].Cells['Res'].Value = $res
}
})
$form.Controls.Add($btnApplyAll)
$btnClose = New-Object System.Windows.Forms.Button
$btnClose.Text = 'Close'
$btnClose.Location = New-Object System.Drawing.Point(820, 555)
$btnClose.Size = New-Object System.Drawing.Size(112, 32)
$btnClose.Anchor = 'Bottom,Right'
$btnClose.Add_Click({ $form.Close() })
$form.Controls.Add($btnClose)
$form.Controls.Add($grid)
Refresh-SenderList
Load-Grid
[void]$form.ShowDialog()