~indicator-multiload/indicator-multiload/trunk

1 by Michael Hofmann
Initial public version.
1
/******************************************************************************
2
 * Copyright (C) 2011  Michael Hofmann <mh21@piware.de>                       *
3
 *                                                                            *
4
 * This program is free software; you can redistribute it and/or modify       *
5
 * it under the terms of the GNU General Public License as published by       *
6
 * the Free Software Foundation; either version 3 of the License, or          *
7
 * (at your option) any later version.                                        *
8
 *                                                                            *
9
 * This program is distributed in the hope that it will be useful,            *
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
12
 * GNU General Public License for more details.                               *
13
 *                                                                            *
14
 * You should have received a copy of the GNU General Public License along    *
15
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                *
17
 ******************************************************************************/
18
19
public class IconData : GLib.Object {
20
    protected IconTraceData[] traces;
21
    private uint _trace_length;
22
    private double[] scalerhistory;
23
    private double scalerminimum;
24
25
    public Gdk.Color color { get; set; }
26
    public uint alpha { get; set; default = 0xffff; }
27
    public bool enabled { get; set; default = true; }
28
    public string id { get; private set; }
29
    public double factor { get; private set; default = 1; }
30
    public uint trace_length {
31
        get {
32
            return this._trace_length;
33
        }
34
        set {
35
            this._trace_length = value;
36
            foreach (unowned IconTraceData trace in this.traces)
37
                trace.set_values_length(value);
38
        }
39
    }
40
41
    public uint length {
42
        get {
43
            return this.traces.length;
44
        }
45
    }
46
47
    // set scalerdelay to 1 and scalerminimum to 1 for percentage values without scaling
48
    public IconData(string id, uint traces, uint scalerdelay, double scalerminimum) {
49
        this.traces = new IconTraceData[traces];
50
        for (uint i = 0; i < traces; ++i)
51
            this.traces[i] = new IconTraceData();
52
        this.id = id;
53
        this.trace_length = 16;
54
        this.scalerhistory = new double[scalerdelay];
55
        for (uint i = 0; i < scalerdelay; ++i)
56
            this.scalerhistory[i] = scalerminimum;
57
        this.scalerminimum = scalerminimum;
58
    }
59
60
    public virtual void update_traces() {
61
        double sum = 0;
62
        foreach (unowned IconTraceData trace in this.traces) {
63
            var next = Random.double_range(0, 1.0 - sum);
64
            sum += next;
65
            trace.add_value(next);
66
        }
67
    }
68
69
    // Fast attack, slow decay
70
    // - each cycle, the peak value in the plot is determined
71
    // - if the peak value is greater than anything in the history buffer, the
72
    //   history buffer is filled with the peak value
73
    // - otherwise, the peak value is added to the history buffer at the end
74
    // - the scaling factor is the average of the history buffer:
75
    //   - it is never smaller than the peak value in the plot
76
    //   - after the current peak leaves the plot, the scaling factor gets
77
    //     reduced slowly
78
    public void update_factor() {
79
        double currentpeak = this.scalerminimum;
80
        for (uint i = 0, isize = this.trace_length; i < isize; ++i) {
81
            double currentvalue = 0;
82
            foreach (unowned IconTraceData trace in this.traces)
83
                currentvalue += trace.values[i];
84
            currentpeak = double.max(currentpeak, currentvalue);
85
        }
86
        double historymaximum = Utils.max(this.scalerhistory);
87
        if (currentpeak < historymaximum) {
88
            for (uint i = 0, isize = this.scalerhistory.length; i + 1 < isize; ++i)
89
                this.scalerhistory[i] = this.scalerhistory[i + 1];
90
        } else {
91
            for (uint i = 0, isize = this.scalerhistory.length; i + 1 < isize; ++i)
92
                this.scalerhistory[i] = currentpeak;
93
        }
94
        this.scalerhistory[this.scalerhistory.length - 1] = currentpeak;
95
        this.factor = Utils.mean(this.scalerhistory);
96
    }
97
98
    public unowned IconTraceData trace(uint index) {
99
        return this.traces[index];
100
    }
101
102
    public void set_source_color(Cairo.Context ctx)
103
    {
104
        ctx.set_source_rgba(this.color.red / 65535.0,
105
                this.color.green / 65565.0,
106
                this.color.blue / 65565.0,
107
                this.alpha / 65565.0);
108
    }
109
}