1 /* $Id$ */
2 /* Copyright (c) 2010-2015 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS Desktop Panel */
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, version 3 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 
17 
18 #include <sys/time.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <time.h>
22 #include <errno.h>
23 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
24 # if defined(__FreeBSD__) || defined(__DragonFly__)
25 #  include <sys/resource.h>
26 # endif
27 # include <sys/sysctl.h>
28 #endif
29 #include <libintl.h>
30 #include <System.h>
31 #include "Panel/applet.h"
32 #define _(string) gettext(string)
33 
34 
35 /* CPU */
36 /* private */
37 /* types */
38 typedef struct _PanelApplet
39 {
40 	PanelAppletHelper * helper;
41 	GtkWidget * widget;
42 	GtkWidget * scale;
43 	guint timeout;
44 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
45 	int used;
46 	int total;
47 #endif
48 } CPU;
49 
50 
51 /* prototypes */
52 static CPU * _cpu_init(PanelAppletHelper * helper, GtkWidget ** widget);
53 static void _cpu_destroy(CPU * cpu);
54 
55 /* accessors */
56 static gboolean _cpu_get(CPU * cpu, gdouble * level);
57 static void _cpu_set(CPU * cpu, gdouble level);
58 
59 /* callbacks */
60 static gboolean _cpu_on_timeout(gpointer data);
61 
62 
63 /* public */
64 /* variables */
65 PanelAppletDefinition applet =
66 {
67 	"CPU",
68 	"gnome-monitor",
69 	NULL,
70 	_cpu_init,
71 	_cpu_destroy,
72 	NULL,
73 	FALSE,
74 	TRUE
75 };
76 
77 
78 /* private */
79 /* functions */
80 /* cpu_init */
_cpu_init(PanelAppletHelper * helper,GtkWidget ** widget)81 static CPU * _cpu_init(PanelAppletHelper * helper, GtkWidget ** widget)
82 {
83 	const int timeout = 500;
84 	CPU * cpu;
85 	PangoFontDescription * desc;
86 	GtkWidget * label;
87 
88 	if((cpu = malloc(sizeof(*cpu))) == NULL)
89 	{
90 		error_set("%s: %s", applet.name, strerror(errno));
91 		return NULL;
92 	}
93 	cpu->helper = helper;
94 #if GTK_CHECK_VERSION(3, 0, 0)
95 	cpu->widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
96 #else
97 	cpu->widget = gtk_hbox_new(FALSE, 0);
98 #endif
99 	desc = pango_font_description_new();
100 	pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD);
101 	label = gtk_label_new(_("CPU:"));
102 #if GTK_CHECK_VERSION(3, 0, 0)
103 	gtk_widget_override_font(label, desc);
104 #else
105 	gtk_widget_modify_font(label, desc);
106 #endif
107 	gtk_box_pack_start(GTK_BOX(cpu->widget), label, FALSE, FALSE, 0);
108 #if GTK_CHECK_VERSION(3, 0, 0)
109 	cpu->scale = gtk_scale_new_with_range(GTK_ORIENTATION_VERTICAL, 0, 100,
110 			1);
111 #else
112 	cpu->scale = gtk_vscale_new_with_range(0, 100, 1);
113 #endif
114 	gtk_widget_set_sensitive(cpu->scale, FALSE);
115 	gtk_range_set_inverted(GTK_RANGE(cpu->scale), TRUE);
116 	gtk_scale_set_value_pos(GTK_SCALE(cpu->scale), GTK_POS_RIGHT);
117 	gtk_box_pack_start(GTK_BOX(cpu->widget), cpu->scale, FALSE, FALSE, 0);
118 	cpu->timeout = g_timeout_add(timeout, _cpu_on_timeout, cpu);
119 	cpu->used = 0;
120 	cpu->total = 0;
121 	_cpu_on_timeout(cpu);
122 	pango_font_description_free(desc);
123 	gtk_widget_show_all(cpu->widget);
124 	*widget = cpu->widget;
125 	return cpu;
126 }
127 
128 
129 /* cpu_destroy */
_cpu_destroy(CPU * cpu)130 static void _cpu_destroy(CPU * cpu)
131 {
132 	if(cpu->timeout > 0)
133 		g_source_remove(cpu->timeout);
134 	gtk_widget_destroy(cpu->widget);
135 	free(cpu);
136 }
137 
138 
139 /* accessors */
140 /* cpu_get */
_cpu_get(CPU * cpu,gdouble * level)141 static gboolean _cpu_get(CPU * cpu, gdouble * level)
142 {
143 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
144 # if defined(__FreeBSD__) || defined(__DragonFly__)
145 	char const name[] = "kern.cp_time";
146 # elif defined(__NetBSD__)
147 	int mib[] = { CTL_KERN, KERN_CP_TIME };
148 # endif
149 	uint64_t cpu_time[CPUSTATES];
150 	size_t size = sizeof(cpu_time);
151 	int used;
152 	int total;
153 
154 # if defined(__FreeBSD__) || defined(__DragonFly__)
155 	if(sysctlbyname(name, &cpu_time, &size, NULL, 0) < 0)
156 # elif defined(__NetBSD__)
157 	if(sysctl(mib, sizeof(mib) / sizeof(*mib), &cpu_time, &size, NULL, 0)
158 			< 0)
159 # endif
160 	{
161 		*level = 0.0 / 0.0;
162 		error_set("%s: %s: %s", applet.name, "sysctl", strerror(errno));
163 		return TRUE;
164 	}
165 	used = cpu_time[CP_USER] + cpu_time[CP_SYS] + cpu_time[CP_NICE]
166 		+ cpu_time[CP_INTR];
167 	total = used + cpu_time[CP_IDLE];
168 	if(cpu->used == 0 || total == cpu->total)
169 		*level = 0.0;
170 	else
171 		*level = 100.0 * (used - cpu->used) / (total - cpu->total);
172 	cpu->used = used;
173 	cpu->total = total;
174 	return TRUE;
175 #else
176 	*level = 0.0 / 0.0;
177 	error_set("%s: %s", applet.name, strerror(ENOSYS));
178 	return FALSE;
179 #endif
180 }
181 
182 
183 /* cpu_set */
_cpu_set(CPU * cpu,gdouble level)184 static void _cpu_set(CPU * cpu, gdouble level)
185 {
186 	gtk_range_set_value(GTK_RANGE(cpu->scale), level);
187 }
188 
189 
190 /* callbacks */
191 /* cpu_on_timeout */
_cpu_on_timeout(gpointer data)192 static gboolean _cpu_on_timeout(gpointer data)
193 {
194 	CPU * cpu = data;
195 	PanelAppletHelper * helper = cpu->helper;
196 	gdouble level;
197 
198 	if(_cpu_get(cpu, &level) == FALSE)
199 	{
200 		cpu->timeout = 0;
201 		helper->error(NULL, error_get(NULL), 1);
202 		return FALSE;
203 	}
204 	_cpu_set(cpu, level);
205 	return TRUE;
206 }
207