-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
120 lines (100 loc) · 2.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"time"
"os"
"os/signal"
"syscall"
"flag"
)
var control_chan = make(chan string)
var (
mode = flag.String("m", "default", "The mode to use. default or aggressive")
show_version = flag.Bool("V", false, "Print mbpfand's version and exit.")
be_verbose = flag.Bool("v", false, "Verbose output.")
update_rate = flag.Int64("u", 10, "Update rate in seconds.")
set_speed = flag.Bool("S", false, "Set fan speed and exit. Speed specified by -s.")
print_speed = flag.Bool("Pf", false, "Print current fan speed and exit.")
print_temp = flag.Bool("Pt", false, "Print current temp and exit.")
force_speed = flag.Int64("s", 2000, "Fan speed for -S option.")
show_usage = flag.Bool("h", false, "Show usage options and exit.")
)
//turns seconds into nanoseconds ... for all the folks who hate zeros
func seconds(n int64) time.Duration {
return (time.Duration(1000000000) * time.Duration(n))
}
func verbOutp(v ...interface{}) {
if *be_verbose {
fmt.Fprintf(os.Stdout, fmt.Sprintln(v...))
}
}
func Usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nOptions:\n")
flag.PrintDefaults()
}
func main() {
flag.Usage = Usage
flag.Parse()
if *show_usage {
flag.Usage()
return
}
if *show_version {
fmt.Println("mbpfand", g_mbpfand_version)
fmt.Println("\t(c) Leon Szpilewski, 2011")
fmt.Println("\tLicensed under GPL v3")
return
}
switch *mode {
case "aggressive":
g_opt_mode = mode_Aggressive
default:
g_opt_mode = mode_Default
}
modes := map[ModeType]string{
mode_Aggressive: "[aggressive]",
mode_Default: "[default]",
}
g_max_fan_speed = readSensor(g_fan_max)
if *set_speed {
SetFanSpeed(float64(*force_speed))
return
}
if *print_speed {
speed := GetFanSpeed()
fmt.Println("Current fan speed:", speed)
if !*print_temp {
return
}
}
if *print_temp {
temp := GetAverageTemp()
fmt.Println("Current Avg Temp:", temp)
return
}
defer SetFanSpeed(g_min_fan_speed)
verbOutp("Using Mode:", modes[g_opt_mode])
verbOutp("Max Fan Speed for this system:", g_max_fan_speed)
verbOutp("Update Rate is:", *update_rate)
//register signal handler
sigchan := make(chan os.Signal)
signal.Notify(sigchan,syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGHUP)
ticker := time.NewTicker(seconds(*update_rate))
L:
for {
select {
case msg := <-control_chan:
if msg == "quit" {
break L
}
case <-ticker.C:
DoWork()
//ok, no idea if this works and why the hell we're doing ths
//I guess some time ago channels got ignored in the select block?
case sig := <-sigchan:
verbOutp("Got signal: " + sig.String())
return
}
}
}