]> www.wagner.pp.ru Git - oss/fubar.git/blob - plugins/apm
*** empty log message ***
[oss/fubar.git] / plugins / apm
1 #!/usr/bin wish
2 if {[file exists /proc/apm]} {
3 namespace eval apm {
4         variable colors
5         variable help
6
7         set c [canvas .apm -width 62 -height 15 -relief sunken -bd 1]
8         pack $c -side right
9         $c create rectangle 10 3 30 13 -outline black
10         $c create rectangle 30 6 33 10 -outline black -fill black
11
12         $c create rectangle 10 3 30 13 -outline black -stipple @[file join $tk_library demos images gray25.bmp] -fill black -tag energy
13
14         $c create polygon 2 1 6 8 3 8 8 15 6 9 9 9 5 1  -outline red -fill red -tag power
15
16         $c create polygon 35 8 38 5 38 11 -fill black -outline black -tag charge
17         $c create polygon 35 5 35 11 38 8 -fill black -outline black -tag discharge
18         $c create text 61 9 -anchor e -text 100% -font 6x10 -tag pwtext
19         bind .apm <Enter> ::apm::start_help
20         bind .apm <Leave> ::apm::cancel_help
21         bind .apm <Motion> ::apm::reset_help
22         array set colors {
23                 charge black
24                 discharge black
25                 power red
26         }
27         proc toggle_item {tag state} {
28                 variable colors
29                 if {$state} {
30                 .apm itemconfig $tag -fill $colors($tag) -outline $colors($tag)
31                 } else {
32                 .apm itemconfig $tag -fill {} -outline {}
33             }
34
35         }
36         proc set_power {percent} {
37                 set x [expr 20*$percent/100+10]
38                 .apm coords energy 10 3 $x 13
39                 .apm itemconfig pwtext -text "$percent%"
40         }
41         proc update {} {
42                 set f [open /proc/apm]
43                 set status [gets $f]
44                 close $f
45                 variable help
46                 set battery [expr [lindex $status 5]]
47                 set help ""
48                 if {[lindex $status 3] == 1} {
49                         toggle_item power 1
50                         toggle_item discharge 0
51                         append help AC 
52                         if {$battery!=255 && ($battery&8)} {
53                                 toggle_item charge 1
54                                 append help " charging [calc_time $status] to complete"
55                         } else {
56                                 toggle_item charge 0
57                                 append help " not charging"
58                         }       
59                         .apm configure -background [lindex [.apm configure -background] 3]
60                 } else {
61                         toggle_item power 0
62                         toggle_item discharge 1
63                         toggle_item charge 0
64                         if {$battery != 255} {
65                                 if {$battery & 1} {
66                                         .apm configure -background yellow
67                                 } elseif {$battery & 2} {
68                                         .apm configure -background #ff7777
69                                 } else {
70                                         .apm configure -background [lindex [.apm configure -background] 3]
71                                 }
72                         }
73                         append help "Battery [calc_time $status] remains" 
74                 }       
75                 set_power [scan [lindex $status 6] "%d%%"]
76                 after 5000 ::apm::update
77                 
78         }
79         proc calc_time {apm_status} {
80                 set units [lindex $apm_status 8]
81                 set count [lindex $apm_status 7]
82                 if {![regexp {[0-9]+} $count]} {
83                         return ??
84                 }       
85                 if {$units == "sec"} {
86                         set sec [expr $count % 60]
87                         set min1 [expr $count / 60]
88                         set min [expr $min1 % 60]
89                         set hours [expr $min1 /60]
90                         return [format "%d:%02d:%02d" $hours $min $sec]
91                 } else {
92                         set min [expr $count % 60]
93                         set hours [expr $count / 60]
94                         return [format "%d:%02d" $hours $min]
95                 }       
96         }
97         proc cancel_help {} {
98                 variable help_after_id
99                 if [info exists help_after_id] {
100                         after cancel $help_after_id
101                         unset help_after_id
102                 }
103                 if {[wm state .apm.help]=="normal"} {
104                         wm withdraw .apm.help
105                 }
106         }
107         proc start_help {} {
108                 variable help_after_id
109                 set help_after_id [after 1000 ::apm::show_help]
110         }
111         proc reset_help {} {
112                 cancel_help
113                 start_help
114         }
115         proc show_help {} {
116                 wm geometry .apm.help +[expr [winfo pointerx .]+2]+[expr [winfo pointery .]+2]
117                 wm deiconify .apm.help
118                 raise .apm.help
119         }       
120                 
121         toplevel .apm.help
122         wm withdraw .apm.help
123         wm overrideredirect .apm.help y
124         label .apm.help.l -textvar ::apm::help -background yellow -font 6x10
125         pack .apm.help.l -side left
126         unset c
127 }
128 }
129 ::apm::update