Scale nbdraid canvas according to screen size.
[libguestfs-talks.git] / 2019-fosdem / nbdview / nbdraid.tcl
1 #!/usr/bin/env wish
2 # Visualize RAID 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
41 # For 1024x768 and 1280x1024 use scale 1
42 # For larger screens use scale 2
43 if { [winfo screenheight .] > 1024 } {
44     set scale 2
45 } else {
46     set scale 1
47 }
48
49 # This list is used to store all the RAID handles.
50 set handles {}
51
52 # This function polls the log file of each handle (every 100 ms).
53 proc poll {} {
54     global handles
55
56     foreach h $handles {
57         nbdpoll $h
58     }
59     after 100 poll
60 }
61
62 # Parse command line.
63 if { $argc != 4 } {
64     puts "usage: nbdview N size logfile-pattern errorfile-pattern"
65     exit 1
66 }
67 set N [lindex $argv 0]
68 set size [lindex $argv 1]
69 set logpattern [lindex $argv 2]
70 set errorpattern [lindex $argv 3]
71
72 # Menubar.
73 option add *tearOff 0
74 menu .menubar
75 . configure -menu .menubar
76 menu .menubar.file
77 .menubar add cascade -menu .menubar.file -label File
78 .menubar.file add command -label "Quit" -command { exit }
79
80 # Frame.
81 frame .f
82 pack .f -anchor center -expand 1 -fill both -ipadx 10 -ipady 10
83
84 # Canvases.
85 for { set i 1 } { $i <= $N } { incr i } {
86     set logfile($i) [format $logpattern $i]
87     set errorfile($i) [format $errorpattern $i]
88
89     # VBox to store the canvas and controls.
90     set box ".f.h$i"
91     frame $box
92     pack $box -side left -expand 1 -fill both
93
94     # Create the error checkbox.
95     set errorbutton "$box.e"
96     set errorvar($i) off
97     checkbutton $errorbutton -text "Error" -variable errorvar($i) \
98         -command "set_errorfile $i"
99     proc set_errorfile { i } {
100         global errorvar errorfile
101         if { $errorvar($i) } {
102             # Touch the error inject file.
103             close [open $errorfile($i) "w"]
104         } else {
105             # Delete it.
106             file delete $errorfile($i)
107         }
108     }
109     pack $errorbutton -side top -anchor nw -expand 0 -fill none
110
111     # Create the i'th canvas.
112     set canvas "$box.c"
113     set h [nbdcanvas $canvas $logfile($i) $size $blocksize $width $scale]
114     $canvas configure -borderwidth 4 -relief groove
115     pack $canvas -side top -expand 0 -fill none
116
117     lappend handles $h
118 }
119
120 # Set up the window.
121 wm attributes . -topmost 1
122 wm title . "nbdraid"
123 wm resizable . false false
124
125 # Start polling the log file.
126 after 100 poll