simplify this conditional a bit
[monky] / src / entropy.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "config.h"
32 #include "conky.h"
33 #include "text_object.h"
34
35 /* check for OS and include appropriate headers */
36 #if defined(__linux__)
37 #include "linux.h"
38 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
39 #include "freebsd.h"
40 #elif defined(__OpenBSD__)
41 #include "openbsd.h"
42 #endif
43
44 static struct {
45         unsigned int avail;
46         unsigned int poolsize;
47 } entropy = {
48         .avail = 0,
49         .poolsize = 0,
50 };
51
52 void update_entropy(void)
53 {
54         get_entropy_avail(&entropy.avail);
55         get_entropy_poolsize(&entropy.poolsize);
56 }
57
58 void print_entropy_avail(struct text_object *obj, char *p, int p_max_size)
59 {
60         (void)obj;
61         snprintf(p, p_max_size, "%u", entropy.avail);
62 }
63
64 void print_entropy_perc(struct text_object *obj, char *p, int p_max_size)
65 {
66         (void)obj;
67         percent_print(p, p_max_size, entropy.avail *
68                         100 / entropy.poolsize);
69 }
70
71 void print_entropy_poolsize(struct text_object *obj, char *p, int p_max_size)
72 {
73         (void)obj;
74         snprintf(p, p_max_size, "%u", entropy.poolsize);
75 }
76
77 void print_entropy_bar(struct text_object *obj, char *p, int p_max_size)
78 {
79         double ratio;
80
81         (void)obj;
82
83         ratio = (double) entropy.avail /
84                 (double) entropy.poolsize;
85         new_bar(obj, p, p_max_size, (int) (ratio * 255.0f));
86 }