| 1 |
// Package v4l, a facade to the Video4Linux video capture interface |
| 2 |
// Copyright (C) 2016 Zoltán Korándi <[email protected]> |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
// Command viewcam displays the video captured from a V4L device in a GTK-based |
| 18 |
// GUI. |
| 19 |
// |
| 20 |
// Usage: |
| 21 |
// viewcam |
| 22 |
package main |
| 23 |
|
| 24 |
import ( |
| 25 |
"errors" |
| 26 |
"fmt" |
| 27 |
"image" |
| 28 |
"strconv" |
| 29 |
|
| 30 |
"github.com/gotk3/gotk3/gdk" |
| 31 |
"github.com/gotk3/gotk3/glib" |
| 32 |
"github.com/gotk3/gotk3/gtk" |
| 33 |
|
| 34 |
"github.com/korandiz/v4l" |
| 35 |
"github.com/korandiz/v4l/fmt/yuyv" |
| 36 |
) |
| 37 |
|
| 38 |
func main() { |
| 39 |
var ( |
| 40 |
dp DevicePickerWindow |
| 41 |
vc VideoCaptureWindow |
| 42 |
cp ControlPanelWindow |
| 43 |
) |
| 44 |
|
| 45 |
dp.OnClose = func() { |
| 46 |
if dp.Video { |
| 47 |
vc.DevicePath = dp.DevicePath |
| 48 |
vc.DeviceConfig = dp.DeviceConfig |
| 49 |
vc.Open() |
| 50 |
} |
| 51 |
if dp.ControlPanel { |
| 52 |
cp.DevicePath = dp.DevicePath |
| 53 |
cp.Open() |
| 54 |
} |
| 55 |
if !dp.Video && !dp.ControlPanel { |
| 56 |
gtk.MainQuit() |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
vc.OnClose = func() { |
| 61 |
cp.Close() |
| 62 |
dp.Open() |
| 63 |
} |
| 64 |
|
| 65 |
cp.OnClose = func() { |
| 66 |
vc.Close() |
| 67 |
dp.Open() |
| 68 |
} |
| 69 |
|
| 70 |
gtk.Init(nil) |
| 71 |
dp.Open() |
| 72 |
gtk.Main() |
| 73 |
} |
| 74 |
|
| 75 |
type DevicePickerWindow struct { |
| 76 |
DevicePath string |
| 77 |
DeviceConfig v4l.DeviceConfig |
| 78 |
Video bool |
| 79 |
ControlPanel bool |
| 80 |
OnClose func() |
| 81 |
|
| 82 |
open bool |
| 83 |
devices []v4l.DeviceInfo |
| 84 |
configs []v4l.DeviceConfig |
| 85 |
ticker *ticker |
| 86 |
|
| 87 |
window *gtk.Window |
| 88 |
deviceList *gtk.ListBox |
| 89 |
configList *gtk.ListBox |
| 90 |
pathLabel *gtk.Label |
| 91 |
nameLabel *gtk.Label |
| 92 |
businfoLabel *gtk.Label |
| 93 |
driverLabel *gtk.Label |
| 94 |
cameraLabel *gtk.Label |
| 95 |
videoButton *gtk.RadioButton |
| 96 |
ctrlButton *gtk.RadioButton |
| 97 |
bothButton *gtk.RadioButton |
| 98 |
} |
| 99 |
|
| 100 |
func (w *DevicePickerWindow) Open() { |
| 101 |
if w.open { |
| 102 |
return |
| 103 |
} |
| 104 |
w.DevicePath = "" |
| 105 |
w.DeviceConfig = v4l.DeviceConfig{} |
| 106 |
w.Video = false |
| 107 |
w.ControlPanel = false |
| 108 |
w.initWidgets() |
| 109 |
w.open = true |
| 110 |
w.updateDeviceList() |
| 111 |
w.ticker = newTicker(500, w.updateDeviceList) |
| 112 |
} |
| 113 |
|
| 114 |
func (w *DevicePickerWindow) Close() { |
| 115 |
if !w.open { |
| 116 |
return |
| 117 |
} |
| 118 |
w.window.Destroy() |
| 119 |
} |
| 120 |
|
| 121 |
func (w *DevicePickerWindow) initWidgets() { |
| 122 |
var err error |
| 123 |
|
| 124 |
w.window, err = gtk.WindowNew(gtk.WINDOW_TOPLEVEL) |
| 125 |
fatal(err) |
| 126 |
w.window.SetTitle("Video Capture Demo") |
| 127 |
w.window.SetIconName("camera-web") |
| 128 |
w.window.SetPosition(gtk.WIN_POS_CENTER) |
| 129 |
w.window.SetSizeRequest(640, 320) |
| 130 |
w.window.SetBorderWidth(6) |
| 131 |
w.window.Connect("destroy", w.windowDestroyed) |
| 132 |
|
| 133 |
grid1, err := gtk.GridNew() |
| 134 |
fatal(err) |
| 135 |
grid1.SetColumnHomogeneous(true) |
| 136 |
grid1.SetColumnSpacing(6) |
| 137 |
grid1.SetRowSpacing(6) |
| 138 |
w.window.Add(grid1) |
| 139 |
|
| 140 |
frame, err := gtk.FrameNew("Devices:") |
| 141 |
fatal(err) |
| 142 |
grid1.Attach(frame, 0, 0, 1, 1) |
| 143 |
|
| 144 |
scrolled, err := gtk.ScrolledWindowNew(nil, nil) |
| 145 |
fatal(err) |
| 146 |
scrolled.SetHExpand(true) |
| 147 |
scrolled.SetVExpand(true) |
| 148 |
frame.Add(scrolled) |
| 149 |
|
| 150 |
w.deviceList, err = gtk.ListBoxNew() |
| 151 |
fatal(err) |
| 152 |
w.deviceList.Connect("selected-rows-changed", w.deviceSelected) |
| 153 |
scrolled.Add(w.deviceList) |
| 154 |
|
| 155 |
frame, err = gtk.FrameNew("Configurations:") |
| 156 |
fatal(err) |
| 157 |
grid1.Attach(frame, 1, 0, 1, 1) |
| 158 |
|
| 159 |
scrolled, err = gtk.ScrolledWindowNew(nil, nil) |
| 160 |
fatal(err) |
| 161 |
scrolled.SetHExpand(true) |
| 162 |
scrolled.SetVExpand(true) |
| 163 |
frame.Add(scrolled) |
| 164 |
|
| 165 |
w.configList, err = gtk.ListBoxNew() |
| 166 |
fatal(err) |
| 167 |
w.configList.Connect("selected-rows-changed", w.configSelected) |
| 168 |
scrolled.Add(w.configList) |
| 169 |
|
| 170 |
grid2, err := gtk.GridNew() |
| 171 |
fatal(err) |
| 172 |
grid2.SetColumnSpacing(12) |
| 173 |
grid2.SetRowSpacing(6) |
| 174 |
grid1.Attach(grid2, 0, 1, 1, 1) |
| 175 |
|
| 176 |
w.pathLabel = labelNew("") |
| 177 |
w.nameLabel = labelNew("") |
| 178 |
w.businfoLabel = labelNew("") |
| 179 |
w.driverLabel = labelNew("") |
| 180 |
w.cameraLabel = labelNew("") |
| 181 |
grid2.Attach(labelNew("Path:"), 0, 0, 1, 1) |
| 182 |
grid2.Attach(labelNew("Name:"), 0, 1, 1, 1) |
| 183 |
grid2.Attach(labelNew("BusInfo:"), 0, 2, 1, 1) |
| 184 |
grid2.Attach(labelNew("Driver:"), 0, 3, 1, 1) |
| 185 |
grid2.Attach(labelNew("Camera:"), 0, 4, 1, 1) |
| 186 |
grid2.Attach(w.pathLabel, 1, 0, 1, 1) |
| 187 |
grid2.Attach(w.nameLabel, 1, 1, 1, 1) |
| 188 |
grid2.Attach(w.businfoLabel, 1, 2, 1, 1) |
| 189 |
grid2.Attach(w.driverLabel, 1, 3, 1, 1) |
| 190 |
grid2.Attach(w.cameraLabel, 1, 4, 1, 1) |
| 191 |
w.clearLabels() |
| 192 |
|
| 193 |
box, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 6) |
| 194 |
fatal(err) |
| 195 |
grid1.Attach(box, 1, 1, 1, 1) |
| 196 |
|
| 197 |
w.videoButton, err = gtk.RadioButtonNewWithLabel(nil, "Video") |
| 198 |
fatal(err) |
| 199 |
box.Add(w.videoButton) |
| 200 |
|
| 201 |
w.ctrlButton, err = gtk.RadioButtonNewWithLabelFromWidget(w.videoButton, |
| 202 |
"Control Panel") |
| 203 |
fatal(err) |
| 204 |
box.Add(w.ctrlButton) |
| 205 |
|
| 206 |
w.bothButton, err = gtk.RadioButtonNewWithLabelFromWidget(w.videoButton, |
| 207 |
"Both") |
| 208 |
fatal(err) |
| 209 |
box.Add(w.bothButton) |
| 210 |
|
| 211 |
button, err := gtk.ButtonNewWithLabel("Start") |
| 212 |
fatal(err) |
| 213 |
button.Connect("clicked", w.startClicked) |
| 214 |
box.Add(button) |
| 215 |
|
| 216 |
w.window.ShowAll() |
| 217 |
} |
| 218 |
|
| 219 |
func (w *DevicePickerWindow) windowDestroyed() { |
| 220 |
w.ticker.cancel() |
| 221 |
*w = DevicePickerWindow{ |
| 222 |
DevicePath: w.DevicePath, |
| 223 |
DeviceConfig: w.DeviceConfig, |
| 224 |
Video: w.Video, |
| 225 |
ControlPanel: w.ControlPanel, |
| 226 |
OnClose: w.OnClose, |
| 227 |
} |
| 228 |
w.OnClose() |
| 229 |
} |
| 230 |
|
| 231 |
func (w *DevicePickerWindow) updateDeviceList() { |
| 232 |
devices := v4l.FindDevices() |
| 233 |
if deviceListsEqual(devices, w.devices) { |
| 234 |
return |
| 235 |
} |
| 236 |
oldIndex := -1 |
| 237 |
newIndex := -1 |
| 238 |
if row := w.deviceList.GetSelectedRow(); row != nil { |
| 239 |
oldIndex = row.GetIndex() |
| 240 |
for i := range devices { |
| 241 |
if devices[i] == w.devices[oldIndex] { |
| 242 |
newIndex = i |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
for i := range w.devices { |
| 247 |
if i < oldIndex || newIndex == -1 { |
| 248 |
w.deviceList.Remove(w.deviceList.GetRowAtIndex(0)) |
| 249 |
} else if i > oldIndex { |
| 250 |
w.deviceList.Remove(w.deviceList.GetRowAtIndex(1)) |
| 251 |
} |
| 252 |
} |
| 253 |
for i := range devices { |
| 254 |
if i < newIndex { |
| 255 |
w.deviceList.Insert(labelNew(devices[i].Path), i) |
| 256 |
} else if i > newIndex { |
| 257 |
w.deviceList.Add(labelNew(devices[i].Path)) |
| 258 |
} |
| 259 |
} |
| 260 |
w.devices = devices |
| 261 |
w.deviceList.ShowAll() |
| 262 |
} |
| 263 |
|
| 264 |
func (w *DevicePickerWindow) deviceSelected() { |
| 265 |
if !w.open { |
| 266 |
return |
| 267 |
} |
| 268 |
w.clearConfigList() |
| 269 |
row := w.deviceList.GetSelectedRow() |
| 270 |
if row == nil { |
| 271 |
w.clearLabels() |
| 272 |
w.DevicePath = "" |
| 273 |
return |
| 274 |
} |
| 275 |
err := w.fillConfigList(w.devices[row.GetIndex()]) |
| 276 |
if err != nil { |
| 277 |
w.modalWarning(err.Error()) |
| 278 |
w.deviceList.SelectRow(nil) |
| 279 |
return |
| 280 |
} |
| 281 |
device := w.devices[row.GetIndex()] |
| 282 |
w.setLabels(device) |
| 283 |
w.DevicePath = device.Path |
| 284 |
} |
| 285 |
|
| 286 |
func (w *DevicePickerWindow) clearConfigList() { |
| 287 |
w.configs = nil |
| 288 |
w.DeviceConfig = v4l.DeviceConfig{} |
| 289 |
for { |
| 290 |
row := w.configList.GetRowAtIndex(0) |
| 291 |
if row == nil { |
| 292 |
break |
| 293 |
} |
| 294 |
w.configList.Remove(row) |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func (w *DevicePickerWindow) clearLabels() { |
| 299 |
w.pathLabel.SetLabel("\u2014") |
| 300 |
w.nameLabel.SetLabel("\u2014") |
| 301 |
w.businfoLabel.SetLabel("\u2014") |
| 302 |
w.driverLabel.SetLabel("\u2014") |
| 303 |
w.cameraLabel.SetLabel("\u2014") |
| 304 |
} |
| 305 |
|
| 306 |
func (w *DevicePickerWindow) setLabels(info v4l.DeviceInfo) { |
| 307 |
dn := info.DriverName |
| 308 |
dv := info.DriverVersion |
| 309 |
c := "No" |
| 310 |
if info.Camera { |
| 311 |
c = "Yes" |
| 312 |
} |
| 313 |
w.pathLabel.SetLabel(info.Path) |
| 314 |
w.nameLabel.SetLabel(info.DeviceName) |
| 315 |
w.businfoLabel.SetLabel(info.BusInfo) |
| 316 |
w.driverLabel.SetLabel(fmt.Sprintf("%s %v.%v.%v", dn, dv[0], dv[1], dv[2])) |
| 317 |
w.cameraLabel.SetLabel(c) |
| 318 |
} |
| 319 |
|
| 320 |
func (w *DevicePickerWindow) fillConfigList(info v4l.DeviceInfo) error { |
| 321 |
dev, err := v4l.Open(info.Path) |
| 322 |
if err != nil { |
| 323 |
return errors.New("Open: " + err.Error()) |
| 324 |
} |
| 325 |
defer dev.Close() |
| 326 |
cfgs, err := dev.ListConfigs() |
| 327 |
if err != nil { |
| 328 |
return errors.New("ListConfigs: " + err.Error()) |
| 329 |
} |
| 330 |
for i := range cfgs { |
| 331 |
if cfgs[i].Format != yuyv.FourCC { |
| 332 |
continue |
| 333 |
} |
| 334 |
w.configList.Add(labelNew(cfg2str(cfgs[i]))) |
| 335 |
w.configs = append(w.configs, cfgs[i]) |
| 336 |
} |
| 337 |
w.configList.ShowAll() |
| 338 |
return nil |
| 339 |
} |
| 340 |
|
| 341 |
func (w *DevicePickerWindow) configSelected() { |
| 342 |
if !w.open { |
| 343 |
return |
| 344 |
} |
| 345 |
row := w.configList.GetSelectedRow() |
| 346 |
if row == nil { |
| 347 |
w.DeviceConfig = v4l.DeviceConfig{} |
| 348 |
return |
| 349 |
} |
| 350 |
w.DeviceConfig = w.configs[row.GetIndex()] |
| 351 |
} |
| 352 |
|
| 353 |
func (w *DevicePickerWindow) startClicked() { |
| 354 |
video := w.videoButton.GetActive() || w.bothButton.GetActive() |
| 355 |
controlPanel := w.ctrlButton.GetActive() || w.bothButton.GetActive() |
| 356 |
if row := w.deviceList.GetSelectedRow(); row == nil { |
| 357 |
w.modalWarning("Please select a device.") |
| 358 |
return |
| 359 |
} |
| 360 |
if row := w.configList.GetSelectedRow(); row == nil && video { |
| 361 |
w.modalWarning("Please select a configuration.") |
| 362 |
return |
| 363 |
} |
| 364 |
w.Video = video |
| 365 |
w.ControlPanel = controlPanel |
| 366 |
w.window.Destroy() |
| 367 |
} |
| 368 |
|
| 369 |
func (w *DevicePickerWindow) modalWarning(msg string) { |
| 370 |
dlg := gtk.MessageDialogNew(w.window, |
| 371 |
gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING, gtk.BUTTONS_OK, msg) |
| 372 |
dlg.SetTitle("Error") |
| 373 |
dlg.SetPosition(gtk.WIN_POS_CENTER_ON_PARENT) |
| 374 |
dlg.Connect("response", dlg.Destroy) |
| 375 |
dlg.ShowAll() |
| 376 |
} |
| 377 |
|
| 378 |
type VideoCaptureWindow struct { |
| 379 |
DevicePath string |
| 380 |
DeviceConfig v4l.DeviceConfig |
| 381 |
OnClose func() |
| 382 |
|
| 383 |
open bool |
| 384 |
capture *capture |
| 385 |
|
| 386 |
window *gtk.Window |
| 387 |
pixbuf *gdk.Pixbuf |
| 388 |
image *gtk.Image |
| 389 |
} |
| 390 |
|
| 391 |
func (w *VideoCaptureWindow) Open() { |
| 392 |
if w.open { |
| 393 |
return |
| 394 |
} |
| 395 |
w.initWidgets() |
| 396 |
w.open = true |
| 397 |
w.capture = newCapture(w) |
| 398 |
} |
| 399 |
|
| 400 |
func (w *VideoCaptureWindow) Close() { |
| 401 |
if !w.open { |
| 402 |
return |
| 403 |
} |
| 404 |
w.window.Destroy() |
| 405 |
} |
| 406 |
|
| 407 |
func (w *VideoCaptureWindow) initWidgets() { |
| 408 |
var err error |
| 409 |
|
| 410 |
w.window, err = gtk.WindowNew(gtk.WINDOW_TOPLEVEL) |
| 411 |
fatal(err) |
| 412 |
w.window.SetTitle("Video Capture Demo") |
| 413 |
w.window.SetIconName("camera-web") |
| 414 |
w.window.SetPosition(gtk.WIN_POS_CENTER) |
| 415 |
w.window.SetResizable(false) |
| 416 |
w.window.Connect("destroy", w.windowDestroyed) |
| 417 |
|
| 418 |
w.pixbuf, err = gdk.PixbufNew(gdk.COLORSPACE_RGB, true, 8, |
| 419 |
w.DeviceConfig.Width, w.DeviceConfig.Height) |
| 420 |
fatal(err) |
| 421 |
for p := w.pixbuf.GetPixels(); len(p) > 0; p = p[1:] { |
| 422 |
p[0] = 0 |
| 423 |
} |
| 424 |
|
| 425 |
w.image, err = gtk.ImageNew() |
| 426 |
fatal(err) |
| 427 |
w.image.SetFromPixbuf(w.pixbuf) |
| 428 |
w.window.Add(w.image) |
| 429 |
|
| 430 |
w.window.ShowAll() |
| 431 |
} |
| 432 |
|
| 433 |
func (w *VideoCaptureWindow) windowDestroyed() { |
| 434 |
w.capture.cancel() |
| 435 |
*w = VideoCaptureWindow{ |
| 436 |
DevicePath: w.DevicePath, |
| 437 |
DeviceConfig: w.DeviceConfig, |
| 438 |
OnClose: w.OnClose, |
| 439 |
} |
| 440 |
w.OnClose() |
| 441 |
} |
| 442 |
|
| 443 |
func (w *VideoCaptureWindow) modalError(prefix string, err error) { |
| 444 |
msg := prefix + ": " + err.Error() |
| 445 |
dlg := gtk.MessageDialogNew(w.window, |
| 446 |
gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, msg) |
| 447 |
dlg.SetTitle("Error") |
| 448 |
dlg.SetPosition(gtk.WIN_POS_CENTER_ON_PARENT) |
| 449 |
dlg.Connect("response", dlg.Destroy) |
| 450 |
dlg.Connect("destroy", w.window.Destroy) |
| 451 |
dlg.ShowAll() |
| 452 |
} |
| 453 |
|
| 454 |
type capture struct { |
| 455 |
cancelled bool |
| 456 |
w *VideoCaptureWindow |
| 457 |
yuyv *yuyv.Image |
| 458 |
} |
| 459 |
|
| 460 |
func newCapture(w *VideoCaptureWindow) *capture { |
| 461 |
c := &capture{w: w} |
| 462 |
go c.captureFrames(w.DevicePath, w.DeviceConfig) |
| 463 |
return c |
| 464 |
} |
| 465 |
|
| 466 |
func (c *capture) cancel() { |
| 467 |
c.cancelled = true |
| 468 |
} |
| 469 |
|
| 470 |
func (c *capture) captureFrames(path string, config v4l.DeviceConfig) { |
| 471 |
device, err := v4l.Open(path) |
| 472 |
if err != nil { |
| 473 |
c.modalError("Open", err) |
| 474 |
return |
| 475 |
} |
| 476 |
defer device.Close() |
| 477 |
if err := device.SetConfig(config); err != nil { |
| 478 |
c.modalError("SetConfig", err) |
| 479 |
return |
| 480 |
} |
| 481 |
if err := device.TurnOn(); err != nil { |
| 482 |
c.modalError("TurnOn", err) |
| 483 |
return |
| 484 |
} |
| 485 |
config, err = device.GetConfig() |
| 486 |
if err != nil { |
| 487 |
c.modalError("GetConfig", err) |
| 488 |
return |
| 489 |
} |
| 490 |
if config.Format != yuyv.FourCC { |
| 491 |
c.modalError("SetConfig", errors.New("failed to set YUYV format")) |
| 492 |
return |
| 493 |
} |
| 494 |
binfo, err := device.BufferInfo() |
| 495 |
if err != nil { |
| 496 |
c.modalError("BufferInfo", err) |
| 497 |
return |
| 498 |
} |
| 499 |
c.yuyv = &yuyv.Image{ |
| 500 |
Pix: make([]byte, binfo.BufferSize), |
| 501 |
Stride: binfo.ImageStride, |
| 502 |
Rect: image.Rect(0, 0, config.Width, config.Height), |
| 503 |
} |
| 504 |
for { |
| 505 |
buffer, err := device.Capture() |
| 506 |
if err != nil { |
| 507 |
c.modalError("Capture", err) |
| 508 |
return |
| 509 |
} |
| 510 |
buffer.ReadAt(c.yuyv.Pix, 0) |
| 511 |
ok := c.updateImage() |
| 512 |
if !ok { |
| 513 |
return |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
func (c *capture) modalError(prefix string, err error) { |
| 519 |
ch := make(chan struct{}) |
| 520 |
glib.IdleAdd(func() { |
| 521 |
if !c.cancelled { |
| 522 |
c.w.modalError(prefix, err) |
| 523 |
} |
| 524 |
ch <- struct{}{} |
| 525 |
}) |
| 526 |
<-ch |
| 527 |
} |
| 528 |
|
| 529 |
func (c *capture) updateImage() bool { |
| 530 |
ch := make(chan bool) |
| 531 |
glib.IdleAdd(func() { |
| 532 |
if c.cancelled { |
| 533 |
ch <- false |
| 534 |
return |
| 535 |
} |
| 536 |
w := c.w |
| 537 |
rgba := &image.RGBA{ |
| 538 |
Pix: w.pixbuf.GetPixels(), |
| 539 |
Stride: w.pixbuf.GetRowstride(), |
| 540 |
Rect: image.Rect(0, 0, w.pixbuf.GetWidth(), w.pixbuf.GetHeight()), |
| 541 |
} |
| 542 |
yuyv.ToRGBA(rgba, rgba.Rect, c.yuyv, c.yuyv.Rect.Min) |
| 543 |
w.image.SetFromPixbuf(c.w.pixbuf) |
| 544 |
ch <- true |
| 545 |
}) |
| 546 |
return <-ch |
| 547 |
} |
| 548 |
|
| 549 |
type ControlPanelWindow struct { |
| 550 |
DevicePath string |
| 551 |
OnClose func() |
| 552 |
|
| 553 |
open bool |
| 554 |
device *v4l.Device |
| 555 |
controls []v4l.ControlInfo |
| 556 |
updaters []updater |
| 557 |
ticker *ticker |
| 558 |
|
| 559 |
window *gtk.Window |
| 560 |
} |
| 561 |
|
| 562 |
type updater func(int32) |
| 563 |
|
| 564 |
func (w *ControlPanelWindow) Open() { |
| 565 |
if w.open { |
| 566 |
return |
| 567 |
} |
| 568 |
w.initWidgets() |
| 569 |
w.open = true |
| 570 |
w.initControls() |
| 571 |
w.updateControls() |
| 572 |
w.ticker = newTicker(100, w.updateControls) |
| 573 |
} |
| 574 |
|
| 575 |
func (w *ControlPanelWindow) Close() { |
| 576 |
if !w.open { |
| 577 |
return |
| 578 |
} |
| 579 |
w.window.Destroy() |
| 580 |
} |
| 581 |
|
| 582 |
func (w *ControlPanelWindow) initWidgets() { |
| 583 |
var err error |
| 584 |
w.window, err = gtk.WindowNew(gtk.WINDOW_TOPLEVEL) |
| 585 |
fatal(err) |
| 586 |
w.window.SetTitle("Control Panel") |
| 587 |
w.window.SetIconName("camera-web") |
| 588 |
w.window.SetPosition(gtk.WIN_POS_CENTER) |
| 589 |
w.window.SetSizeRequest(640, 320) |
| 590 |
w.window.Connect("destroy", w.windowDestroyed) |
| 591 |
w.window.ShowAll() |
| 592 |
} |
| 593 |
|
| 594 |
func (w *ControlPanelWindow) initControls() { |
| 595 |
var err error |
| 596 |
w.device, err = v4l.Open(w.DevicePath) |
| 597 |
if err != nil { |
| 598 |
w.modalError("Open", err) |
| 599 |
return |
| 600 |
} |
| 601 |
w.controls, err = w.device.ListControls() |
| 602 |
if err != nil { |
| 603 |
w.modalError("ListControls", err) |
| 604 |
return |
| 605 |
} |
| 606 |
notebook, err := gtk.NotebookNew() |
| 607 |
fatal(err) |
| 608 |
notebook.SetScrollable(true) |
| 609 |
notebook.SetShowBorder(false) |
| 610 |
w.window.Add(notebook) |
| 611 |
tabs := make(map[uint32]*gtk.Grid) |
| 612 |
for i, ctrl := range w.controls { |
| 613 |
class := (ctrl.CID & 0x0fff0000) >> 16 |
| 614 |
grid, ok := tabs[class] |
| 615 |
if !ok { |
| 616 |
grid, err = gtk.GridNew() |
| 617 |
fatal(err) |
| 618 |
grid.SetBorderWidth(12) |
| 619 |
grid.SetColumnSpacing(12) |
| 620 |
grid.SetRowSpacing(12) |
| 621 |
grid.SetRowHomogeneous(true) |
| 622 |
grid.SetVAlign(gtk.ALIGN_START) |
| 623 |
className := ctrlClassNames[class] |
| 624 |
if len(className) == 0 { |
| 625 |
className = fmt.Sprintf("Class %03x", class) |
| 626 |
} |
| 627 |
notebook.AppendPage(grid, labelNew(className)) |
| 628 |
tabs[class] = grid |
| 629 |
} |
| 630 |
|
| 631 |
frame, err := gtk.FrameNew("") |
| 632 |
fatal(err) |
| 633 |
if ctrl.Type != "button" { |
| 634 |
grid.Attach(labelNew(ctrl.Name), 0, i, 1, 1) |
| 635 |
} |
| 636 |
if ctrl.Type != "int" { |
| 637 |
frame.SetHAlign(gtk.ALIGN_START) |
| 638 |
frame.SetVAlign(gtk.ALIGN_CENTER) |
| 639 |
} |
| 640 |
frame.SetShadowType(gtk.SHADOW_NONE) |
| 641 |
grid.Attach(frame, 1, i, 1, 1) |
| 642 |
var upd updater |
| 643 |
switch ctrl.Type { |
| 644 |
case "int": |
| 645 |
upd = w.createIntControl(&frame.Container, ctrl) |
| 646 |
case "bool": |
| 647 |
upd = w.createBoolControl(&frame.Container, ctrl) |
| 648 |
case "enum": |
| 649 |
upd = w.createEnumControl(&frame.Container, ctrl) |
| 650 |
case "button": |
| 651 |
upd = w.createButtonControl(&frame.Container, ctrl) |
| 652 |
} |
| 653 |
w.updaters = append(w.updaters, upd) |
| 654 |
} |
| 655 |
notebook.ShowAll() |
| 656 |
} |
| 657 |
|
| 658 |
var ctrlClassNames = map[uint32]string{ |
| 659 |
0x098: "User Controls", |
| 660 |
0x099: "Codec", |
| 661 |
0x09a: "Camera", |
| 662 |
0x09b: "FM Modulator", |
| 663 |
0x09c: "Flash", |
| 664 |
0x09d: "JPEG", |
| 665 |
0x09e: "Image Source", |
| 666 |
0x09f: "Image Processing", |
| 667 |
0x0a0: "Digital Video", |
| 668 |
0x0a1: "FM Receiver", |
| 669 |
0x0a2: "RF Tuner", |
| 670 |
0x0a3: "Detection", |
| 671 |
0x0a4: "Stateless Codec", |
| 672 |
0x0a5: "Colorimetry", |
| 673 |
} |
| 674 |
|
| 675 |
func (w *ControlPanelWindow) createIntControl(parent *gtk.Container, ctrl v4l.ControlInfo) updater { |
| 676 |
var err error |
| 677 |
box, err := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 6) |
| 678 |
fatal(err) |
| 679 |
parent.Add(box) |
| 680 |
scale, err := gtk.ScaleNewWithRange(gtk.ORIENTATION_HORIZONTAL, |
| 681 |
float64(ctrl.Min), float64(ctrl.Max), float64(ctrl.Step)) |
| 682 |
fatal(err) |
| 683 |
scale.SetHExpand(true) |
| 684 |
scale.SetProperty("draw-value", false) |
| 685 |
box.Add(scale) |
| 686 |
frame, err := gtk.FrameNew("") |
| 687 |
fatal(err) |
| 688 |
frame.SetShadowType(gtk.SHADOW_NONE) |
| 689 |
frame.SetSizeRequest(50, 0) |
| 690 |
box.Add(frame) |
| 691 |
label, err := gtk.LabelNew("") |
| 692 |
fatal(err) |
| 693 |
label.SetHAlign(gtk.ALIGN_END) |
| 694 |
frame.Add(label) |
| 695 |
scale.Connect("value-changed", func() { |
| 696 |
value := scale.GetValue() |
| 697 |
if value >= 0 { |
| 698 |
value += 0.5 |
| 699 |
} else { |
| 700 |
value -= 0.5 |
| 701 |
} |
| 702 |
w.device.SetControl(ctrl.CID, int32(value)) |
| 703 |
w.updateControls() |
| 704 |
}) |
| 705 |
return func(value int32) { |
| 706 |
scale.SetValue(float64(value)) |
| 707 |
label.SetLabel(strconv.Itoa(int(value))) |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
func (w *ControlPanelWindow) createBoolControl(parent *gtk.Container, ctrl v4l.ControlInfo) updater { |
| 712 |
checkButton, err := gtk.CheckButtonNew() |
| 713 |
fatal(err) |
| 714 |
checkButton.SetHExpand(false) |
| 715 |
checkButton.SetVExpand(false) |
| 716 |
parent.Add(checkButton) |
| 717 |
checkButton.Connect("toggled", func() { |
| 718 |
value := int32(0) |
| 719 |
if checkButton.GetActive() { |
| 720 |
value = 1 |
| 721 |
} |
| 722 |
w.device.SetControl(ctrl.CID, value) |
| 723 |
w.updateControls() |
| 724 |
}) |
| 725 |
return func(value int32) { |
| 726 |
active := false |
| 727 |
if value != 0 { |
| 728 |
active = true |
| 729 |
} |
| 730 |
checkButton.SetActive(active) |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
func (w *ControlPanelWindow) createEnumControl(parent *gtk.Container, ctrl v4l.ControlInfo) updater { |
| 735 |
comboBox, err := gtk.ComboBoxTextNew() |
| 736 |
fatal(err) |
| 737 |
parent.Add(comboBox) |
| 738 |
for _, opt := range ctrl.Options { |
| 739 |
comboBox.Append("", opt.Name) |
| 740 |
} |
| 741 |
comboBox.Connect("changed", func() { |
| 742 |
value := ctrl.Options[comboBox.GetActive()].Value |
| 743 |
w.device.SetControl(ctrl.CID, value) |
| 744 |
w.updateControls() |
| 745 |
}) |
| 746 |
return func(value int32) { |
| 747 |
for i, opt := range ctrl.Options { |
| 748 |
if opt.Value == value { |
| 749 |
comboBox.SetActive(i) |
| 750 |
break |
| 751 |
} |
| 752 |
} |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
func (w *ControlPanelWindow) createButtonControl(parent *gtk.Container, ctrl v4l.ControlInfo) updater { |
| 757 |
button, err := gtk.ButtonNewWithLabel(ctrl.Name) |
| 758 |
fatal(err) |
| 759 |
parent.Add(button) |
| 760 |
button.Connect("clicked", func() { |
| 761 |
w.device.SetControl(ctrl.CID, 0) |
| 762 |
w.updateControls() |
| 763 |
}) |
| 764 |
return nil |
| 765 |
} |
| 766 |
|
| 767 |
func (w *ControlPanelWindow) windowDestroyed() { |
| 768 |
w.ticker.cancel() |
| 769 |
if w.device != nil { |
| 770 |
w.device.Close() |
| 771 |
} |
| 772 |
*w = ControlPanelWindow{ |
| 773 |
DevicePath: w.DevicePath, |
| 774 |
OnClose: w.OnClose, |
| 775 |
} |
| 776 |
w.OnClose() |
| 777 |
} |
| 778 |
|
| 779 |
func (w *ControlPanelWindow) updateControls() { |
| 780 |
for i, ctrl := range w.controls { |
| 781 |
if ctrl.Type == "button" { |
| 782 |
continue |
| 783 |
} |
| 784 |
value, err := w.device.GetControl(ctrl.CID) |
| 785 |
if err != nil { |
| 786 |
continue |
| 787 |
} |
| 788 |
if w.updaters[i] != nil { |
| 789 |
w.updaters[i](value) |
| 790 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
func (w *ControlPanelWindow) modalError(prefix string, err error) { |
| 795 |
msg := prefix + ": " + err.Error() |
| 796 |
dlg := gtk.MessageDialogNew(w.window, |
| 797 |
gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, msg) |
| 798 |
dlg.SetTitle("Error") |
| 799 |
dlg.SetPosition(gtk.WIN_POS_CENTER_ON_PARENT) |
| 800 |
dlg.Connect("response", dlg.Destroy) |
| 801 |
dlg.Connect("destroy", w.window.Destroy) |
| 802 |
dlg.ShowAll() |
| 803 |
} |
| 804 |
|
| 805 |
type ticker struct { |
| 806 |
interval uint |
| 807 |
callback func() |
| 808 |
cancelled bool |
| 809 |
} |
| 810 |
|
| 811 |
func newTicker(interval uint, callback func()) *ticker { |
| 812 |
t := &ticker{ |
| 813 |
interval: interval, |
| 814 |
callback: callback, |
| 815 |
} |
| 816 |
glib.TimeoutAdd(interval, t.handler) |
| 817 |
return t |
| 818 |
} |
| 819 |
|
| 820 |
func (t *ticker) cancel() { |
| 821 |
t.cancelled = true |
| 822 |
} |
| 823 |
|
| 824 |
func (t *ticker) handler() bool { |
| 825 |
if t.cancelled { |
| 826 |
return false |
| 827 |
} |
| 828 |
t.callback() |
| 829 |
return true |
| 830 |
|
| 831 |
} |
| 832 |
|
| 833 |
func deviceListsEqual(d1, d2 []v4l.DeviceInfo) bool { |
| 834 |
if len(d1) != len(d2) { |
| 835 |
return false |
| 836 |
} |
| 837 |
for i := range d1 { |
| 838 |
if d1[i] != d2[i] { |
| 839 |
return false |
| 840 |
} |
| 841 |
} |
| 842 |
return true |
| 843 |
} |
| 844 |
|
| 845 |
func cfg2str(cfg v4l.DeviceConfig) string { |
| 846 |
w := cfg.Width |
| 847 |
h := cfg.Height |
| 848 |
f := cfg.FPS |
| 849 |
return fmt.Sprintf("%dx%d @ %.4g FPS", w, h, float64(f.N)/float64(f.D)) |
| 850 |
} |
| 851 |
|
| 852 |
func labelNew(s string) *gtk.Label { |
| 853 |
l, err := gtk.LabelNew(s) |
| 854 |
fatal(err) |
| 855 |
l.SetHAlign(gtk.ALIGN_START) |
| 856 |
return l |
| 857 |
} |
| 858 |
|
| 859 |
func fatal(err error) { |
| 860 |
if err != nil { |
| 861 |
panic(err) |
| 862 |
} |
| 863 |
} |