~vitorafsr/i8kutils/trunk

« back to all changes in this revision

Viewing changes to i8kui

  • Committer: Vitor Augusto
  • Date: 2017-03-05 14:28:50 UTC
  • Revision ID: vitorafsr@gmail.com-20170305142850-xus9xpw6avcysyw8
Some cleanup, and removed obsolete content.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env tclsh
2
 
#
3
 
# i8kui -- User interface for i8kutils
4
 
#
5
 
# Copyright (C) 2014-2015 Vitor Augusto <vitorafsr@gmail.com>
6
 
#
7
 
# This program is free software; you can redistribute it and/or modify it
8
 
# under the terms of the GNU General Public License as published by the
9
 
# Free Software Foundation; either version 2, or (at your option) any
10
 
# later version.
11
 
#
12
 
# This program is distributed in the hope that it will be useful, but
13
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
# General Public License for more details.
16
 
 
17
 
 
18
 
array set config {
19
 
    sysconfig   /etc/i8kmon.conf
20
 
    userconfig  ~/.i8kmon
21
 
    use_conf    1
22
 
    geometry    {}    
23
 
    timeout 5
24
 
    t_high  80
25
 
    unit    C
26
 
    verbose 0
27
 
}
28
 
 
29
 
array set status {    
30
 
    lstate  0
31
 
    rstate  0
32
 
    bg  white
33
 
    activebg    white
34
 
    nfans   1
35
 
}
36
 
 
37
 
proc read_config {} {
38
 
    global config
39
 
    global status
40
 
 
41
 
    # read system config file
42
 
    if {[file exists $config(sysconfig)]} {
43
 
        source $config(sysconfig)
44
 
        if {$config(verbose) > 0} {
45
 
            puts "reading system config file"
46
 
        }
47
 
    }
48
 
 
49
 
    # read user config file
50
 
    # as it is read last, the options here have precedence
51
 
    if {$config(use_conf) != 0} {
52
 
        if {[file exists $config(userconfig)]} {
53
 
            source $config(userconfig)
54
 
            if {$config(verbose) > 0} {
55
 
                puts "reading user config file"
56
 
            }
57
 
        }
58
 
    }
59
 
}
60
 
 
61
 
proc status_timer {} {
62
 
    global config
63
 
 
64
 
    # Reschedule status timer
65
 
    after [expr $config(timeout)*1000] {status_timer}
66
 
 
67
 
    update_ui
68
 
}
69
 
 
70
 
 
71
 
proc make_ui {} {
72
 
    global status
73
 
    global config
74
 
    global tcl_version
75
 
 
76
 
    if {[info command .i8kui] != {}} {
77
 
        # Applet aready existing
78
 
        return
79
 
    }
80
 
 
81
 
    # Load Tk library (require a Tk version >=8.4)
82
 
    package require Tk 8.4
83
 
    wm withdraw .
84
 
    update
85
 
 
86
 
    toplevel .i8kui -class I8kui -relief sunken -bd 1
87
 
    wm title .i8kui i8kmon
88
 
    wm command .i8kui i8kmon
89
 
    wm protocol .i8kui WM_DELETE_WINDOW { exit }
90
 
 
91
 
    if {$config(geometry) != {}} {
92
 
        set geometry $config(geometry)
93
 
    } else {
94
 
        set geometry 48x48
95
 
    }
96
 
    if {[lindex [split $geometry x] 1] >= 36} {
97
 
        set font fixed
98
 
    } else {
99
 
        set font 6x10
100
 
    }
101
 
    wm geometry .i8kui $geometry
102
 
 
103
 
    label .i8kui.temp -bd 0 -padx 0 -pady 0 -text "0" -font $font \
104
 
        -highlightthickness 0 -width 3
105
 
    button .i8kui.lfan -bd 1 -padx 0 -pady 0 -text {} -font $font \
106
 
        -highlightthickness 0 -command {toggle_fan left}
107
 
    button .i8kui.rfan -bd 1 -padx 0 -pady 0 -text {} -font $font \
108
 
        -highlightthickness 0 -command {toggle_fan right}
109
 
    bind .i8kui.lfan <Button-2> {toggle_fan left 2}
110
 
    bind .i8kui.lfan <Button-3> {toggle_fan left 0}
111
 
    bind .i8kui.rfan <Button-2> {toggle_fan right 2}
112
 
    bind .i8kui.rfan <Button-3> {toggle_fan right 0}
113
 
 
114
 
    place .i8kui.temp -relx 0.0 -rely 0.0 -relwidth 1.0 -relheight 0.5
115
 
    place .i8kui.lfan -relx 0.0 -rely 0.5 -relwidth 0.5 -relheight 0.5
116
 
    place .i8kui.rfan -relx 0.5 -rely 0.5 -relwidth 0.5 -relheight 0.5
117
 
 
118
 
    # Set $status(lstate) and $status(rstate)
119
 
    set x [eval exec i8kfan]
120
 
    set status(lstate) [lindex $x 0]
121
 
    set status(rstate) [lindex $x 1]
122
 
    if {$status(lstate) < 0} {
123
 
        place forget .i8kui.lfan
124
 
        place .i8kui.rfan -relx 0 -rely 0.5 -relwidth 1 -relheight 0.5
125
 
    }
126
 
    if {$status(rstate) < 0} {
127
 
        place forget .i8kui.rfan
128
 
        place .i8kui.lfan -relx 0 -rely 0.5 -relwidth 1 -relheight 0.5
129
 
    }
130
 
 
131
 
    set status(bg)       [.i8kui.lfan cget -bg]
132
 
    set status(activebg) [.i8kui.lfan cget -activebackground]
133
 
 
134
 
    set menu .i8kui.menu
135
 
    menu $menu -tearoff 0
136
 
    $menu add check -label "Verbose" -variable config(verbose)
137
 
    $menu add separator
138
 
    $menu add command -label "Reload" -command { read_config; update_ui }
139
 
    $menu add command -label "Exit"   -command { exit }
140
 
 
141
 
    bind .i8kui.temp <Button-1> "tk_popup $menu %X %Y"
142
 
 
143
 
    update
144
 
}
145
 
 
146
 
proc update_ui {} {
147
 
    global config
148
 
    global status
149
 
 
150
 
    # If applet window is unexpectedly destroyed exit the program
151
 
    if {![winfo exists .i8kui]} {
152
 
        puts stderr "applet window destroyed, exit"
153
 
        exit 1
154
 
    }
155
 
 
156
 
    set bg $status(bg)
157
 
    set ab $status(activebg)
158
 
 
159
 
    # Temperature
160
 
    set temp [eval exec "i8kctl temp"]
161
 
    if {$temp >= $config(t_high)} {
162
 
        set fg red
163
 
    } else {
164
 
        set fg black
165
 
    }
166
 
    if {$config(unit) == "F" } {
167
 
        .i8kui.temp config -text [expr (0+$temp)*9/5+32] -fg $fg
168
 
    } else {
169
 
        .i8kui.temp config -text $temp -fg $fg
170
 
    }
171
 
 
172
 
    # Left button    
173
 
    set x [eval exec i8kfan]
174
 
    set status(lstate) [lindex $x 0]
175
 
    set status(rstate) [lindex $x 1]
176
 
    if {$status(lstate) == 0} {
177
 
        .i8kui.lfan config -text {} -bg $bg -activebackground $ab
178
 
    } elseif {$status(lstate) >= 2} {
179
 
        .i8kui.lfan config -text $status(lstate) -bg red -activebackground red
180
 
    } else {
181
 
        .i8kui.lfan config -text $status(lstate) -bg $bg -activebackground $ab
182
 
    }
183
 
 
184
 
    # Right button
185
 
    if {$status(nfans) < 2} { return }
186
 
    if {$status(rstate) == 0} {
187
 
        .i8kui.rfan config -text {} -bg $bg -activebackground $ab
188
 
    } elseif {$status(rstate) >= 2} {
189
 
        .i8kui.rfan config -text $status(rstate) -bg red -activebackground red
190
 
    } else {
191
 
        .i8kui.rfan config -text $status(rstate) -bg $bg -activebackground $ab
192
 
    }
193
 
 
194
 
    update
195
 
}
196
 
 
197
 
proc toggle_fan {fan {speed {}}} {
198
 
    global status
199
 
 
200
 
    set status($fan) 0
201
 
    if {$speed != {}} {
202
 
        set status($fan) $speed
203
 
    } else {
204
 
        if {$fan == "left"} {
205
 
            set status($fan) $status(lstate)
206
 
        } else {
207
 
            set status($fan) $status(rstate)
208
 
        }
209
 
        set status($fan) [expr ($status($fan)+1) % 3]
210
 
    }
211
 
 
212
 
    set status($fan) [expr ($status($fan)+1) % 3]
213
 
 
214
 
    if {$fan == "left"} {
215
 
          exec i8kfan $status($fan) "-"
216
 
    } else {
217
 
          exec i8kfan "-" $status($fan)
218
 
    }
219
 
}
220
 
 
221
 
if {$tcl_interactive == 0} {
222
 
    read_config
223
 
    make_ui
224
 
    status_timer
225
 
    vwait forever
226
 
}