fa6343e55406023f4e9d60617fd5d55800c86696
[libguestfs-talks.git] / 2019-fosdem / nbdview / nbdview.tcl
1 #!/usr/bin/env wish
2 # Visualize nbdkit activity - read the README file first!
3 # Copyright (C) 2018 Red Hat Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 #
17 # * Neither the name of Red Hat nor the names of its contributors may be
18 # used to endorse or promote products derived from this software without
19 # specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
25 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 # SUCH DAMAGE.
33
34 package require Tk
35
36 source "nbdcanvas.tcl"
37
38 set blocksize 4096
39 set width 128
40 set scale 2
41
42 # This is used to store the canvas handle.
43 set h ""
44
45 # This function polls the log file (every 100 ms).
46 proc poll {} {
47     global h
48
49     nbdpoll $h
50     after 100 poll
51 }
52
53 # Parse command line.
54 if { $argc != 2 } {
55     puts "usage: nbdview logfile size"
56     exit 1
57 }
58
59 set logfile [lindex $argv 0]
60 set size [lindex $argv 1]
61
62 # Set up the window.
63 wm title . "nbdview $logfile"
64
65 # Menubar.
66 option add *tearOff 0
67 menu .menubar
68 . configure -menu .menubar
69 menu .menubar.file
70 .menubar add cascade -menu .menubar.file -label File
71 .menubar.file add command -label "Quit" -command { exit }
72
73 # Canvas.
74 set h [nbdcanvas .c $logfile $size $blocksize $width $scale]
75 pack .c -in . -expand 1 -fill both
76
77 # Set the width and height of the window.
78 set h_ [expr {[nbdheight $h] * $scale + 40}]
79 set w [expr {$width * $scale + 16}]
80 wm geometry . "${w}x${h_}"
81 wm resizable . false false
82
83 # Start polling the log file.
84 after 100 poll