2 * graph.c - a graph widget
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2007-2008 Marco Candrian <mac@calmar.ws>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 extern AwesomeConf globalconf
;
33 int percent
; /* Percent 0 to 100 */
34 int width
; /* Width of the widget */
35 int padding_left
; /* Left padding */
36 float height
; /* Height 0-1, where 1 is height of statusbar */
37 XColor fg
; /* Foreground color */
38 XColor bg
; /* Background color */
39 XColor bordercolor
; /* Border color */
41 int *lines
; /* keeps the calculated values (line-length); */
42 int lines_index
; /* pointer to current val */
43 int lines_size
; /* size of lines-array */
44 float *line_values
; /* actual values */
45 int line_max_index
; /* index of the current maximum value */
46 int line_max
; /* maximum value */
52 graph_draw(Widget
*widget
, DrawCtx
*ctx
, int offset
,
53 int used
__attribute__ ((unused
)))
55 int width
, height
; /* width/height (pixel) to draw the outer box */
56 int margin_top
, left_offset
;
57 Data
*d
= widget
->data
;
59 width
= d
->lines_size
+ 2;
61 if(!widget
->user_supplied_x
)
62 widget
->area
.x
= widget_calculate_offset(widget
->statusbar
->width
,
66 if(!widget
->user_supplied_y
)
69 margin_top
= (int) (widget
->statusbar
->height
* (1 - d
->height
)) / 2 + 0.5 + widget
->area
.y
;
70 height
= (int) (widget
->statusbar
->height
* d
->height
+ 0.5);
71 left_offset
= widget
->area
.x
+ d
->padding_left
;
74 left_offset
, margin_top
,
76 False
, d
->bordercolor
);
79 left_offset
+ 1, margin_top
+ 1,
80 width
- 2, height
- 2,
83 /* computes line-length to draw and store into lines[] */
84 d
->lines
[d
->lines_index
] = (int) (d
->percent
) * (height
- 2 ) / 100 + 0.5;
86 if(d
->lines
[d
->lines_index
] < 0)
87 d
->lines
[d
->lines_index
] = 0;
90 left_offset
+ 2, margin_top
+ height
- 1,
91 d
->lines_size
, d
->lines
, d
->lines_index
,
94 widget
->area
.width
= d
->width
;
95 widget
->area
.height
= widget
->statusbar
->height
;
96 return widget
->area
.width
;
100 graph_tell(Widget
*widget
, char *command
)
102 Data
*d
= widget
->data
;
103 int percent
, i
, height
, omax
, nmax
;
105 height
= (int) (widget
->statusbar
->height
* d
->height
+ 0.5);
107 if(!command
|| d
->width
< 1)
110 if(++d
->lines_index
>= d
->lines_size
) /* cycle inside the array */
113 if (d
->line_values
&& d
->line_max_index
== d
->lines_index
&&
114 d
->line_values
[d
->line_max_index
]) {
115 omax
= d
->line_values
[d
->line_max_index
];
116 d
->line_values
[d
->line_max_index
] = 0;
117 for (i
= 0; i
< d
->lines_size
; i
++)
118 if (d
->line_values
[i
] > d
->line_values
[d
->line_max_index
]) {
119 d
->line_max_index
= i
;
121 nmax
= MAX(d
->line_values
[d
->line_max_index
], d
->line_max
);
122 if (d
->line_values
[d
->line_max_index
]) {
123 for (i
= 0; i
< d
->lines_size
; i
++) {
124 d
->lines
[i
] = (int) (100*d
->line_values
[i
] / nmax
) * (height
-
130 percent
= atoi(command
);
134 d
->line_values
[d
->lines_index
] = 0;
137 else if (d
->line_values
)
139 d
->line_values
[d
->lines_index
] = percent
;
140 nmax
= MAX(d
->line_max
, d
->line_values
[d
->line_max_index
]);
141 if (d
->line_values
[d
->line_max_index
] < percent
) {
142 // Increase the current max
143 d
->line_max_index
= d
->lines_index
;
144 if (d
->line_max
< d
->line_values
[d
->line_max_index
]) {
145 for (i
= 0; i
< d
->lines_size
; i
++) {
147 (100*d
->line_values
[i
]/d
->line_values
[d
->line_max_index
])
148 * (height
- 2) / 100 + 0.5;
152 d
->percent
= (int) (100.0 * d
->line_values
[d
->lines_index
] / nmax
);
155 d
->percent
= (percent
> d
->line_max
? 100 :
156 (int) (100 * ((float)percent
) / d
->line_max
));
161 graph_new(Statusbar
*statusbar
, cfg_t
*config
)
166 int phys_screen
= get_phys_screen(statusbar
->screen
);
168 w
= p_new(Widget
, 1);
169 widget_common_new(w
, statusbar
, config
);
171 w
->draw
= graph_draw
;
172 w
->tell
= graph_tell
;
173 d
= w
->data
= p_new(Data
, 1);
175 d
->height
= cfg_getfloat(config
, "height");
176 d
->width
= cfg_getint(config
, "width");
177 d
->padding_left
= cfg_getint(config
, "padding_left");
179 d
->lines_size
= d
->width
- d
->padding_left
- 2;
180 d
->lines_index
= -1; /* graph_tell will increment it to 0 (to begin with...) */
182 if(d
->lines_size
< 1)
184 warn("graph widget needs: (width - padding_left) >= 3\n");
188 d
->lines
= p_new(int, d
->lines_size
);
189 if (cfg_getbool(config
, "scale"))
190 d
->line_values
= p_new(float, d
->lines_size
);
191 d
->line_max
= cfg_getfloat(config
, "max");
193 if((color
= cfg_getstr(config
, "fg")))
194 d
->fg
= initxcolor(phys_screen
, color
);
196 d
->fg
= globalconf
.screens
[statusbar
->screen
].colors_normal
[ColFG
];
198 if((color
= cfg_getstr(config
, "bg")))
199 d
->bg
= initxcolor(phys_screen
, color
);
201 d
->bg
= globalconf
.screens
[statusbar
->screen
].colors_normal
[ColBG
];
203 if((color
= cfg_getstr(config
, "bordercolor")))
204 d
->bordercolor
= initxcolor(phys_screen
, color
);
206 d
->bordercolor
= d
->fg
;
210 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80