42 #include <libdockapp/wmgeneral.h> 43 #include <libdockapp/misc.h> 46 #include "wmcpuwatch-master.xpm" 47 #include "wmcpuwatch-mask.xbm" 52 #define MAX_HEIGHT (9) 83 void get_statistics(FILE *, ullong *, ullong *, ullong *, ullong *);
85 unsigned long get_width(
long,
long,
long);
108 int main(
int argc,
char *argv[])
110 char *name = argv[0];
113 for (
int i = 1; i < argc; i++) {
119 if (strcmp(arg+1,
"display")) {
125 if (strcmp(arg+1,
"geometry")) {
155 ullong istat, idle, *istat2, *idle2;
160 fp_stat = fopen(
"/proc/stat",
"r");
165 istat2 = calloc(nb_cpu,
sizeof(ullong));
166 idle2 = calloc(nb_cpu,
sizeof(ullong));
167 if (!istat2 || !idle2) {
168 fprintf(stderr,
"%s: Unable to alloc memory !!\n", argv[0]);
172 openXwindow(argc, argv, wmcpuwatch_master_xpm, (
char *)wmcpuwatch_mask_bits,
173 wmcpuwatch_mask_width, wmcpuwatch_mask_height);
180 for (
int cpu = 0; cpu < nb_cpu; cpu++) {
181 stat_device.
cpu_last[cpu] = istat2[cpu];
186 waitpid(0, NULL, WNOHANG);
195 copyXPMArea(32, 64, j, 12, 28, 4);
199 for (
int cpu = 0; cpu < nb_cpu; cpu++) {
201 copyXPMArea(1, 95, j, h, 5, 19 + h * cpu);
206 while (XPending(display)) {
207 XNextEvent(display, &Event);
208 switch (Event.type) {
213 XCloseDisplay(display);
233 copyXPMArea(0, 64, 32, 12, 28, 4);
236 copyXPMArea(0, 80, 56, 10, 4, 18);
237 copyXPMArea(0, 81, 56, 9, 4, 27);
238 copyXPMArea(0, 81, 56, 9, 4, 36);
239 copyXPMArea(0, 81, 56, 9, 4, 45);
240 copyXPMArea(0, 81, 56, 4, 4, 54);
241 copyXPMArea(0, 75, 56, 1, 4, 58);
255 statdevice->
cpu_stat = calloc(cpucount,
sizeof(
long));
256 statdevice->
cpu_last = calloc(cpucount,
sizeof(ullong));
257 statdevice->
idle_stat = calloc(cpucount,
sizeof(
long));
258 statdevice->
idle_last = calloc(cpucount,
sizeof(ullong));
263 fprintf(stderr,
"%s: Unable to alloc memory !\n", dockappname);
290 for (
int cpu = 0; cpu < cpucount; cpu++) {
311 void get_statistics(FILE *fpstat, ullong *ds, ullong *idle, ullong *ds2, ullong *idle2)
313 static char *line = NULL;
314 static size_t line_size = 0;
316 char *tokens =
" \t\n";
323 fseek(fpstat, 0, SEEK_SET);
324 while ((getline(&line, &line_size, fpstat)) > 0) {
325 if (strstr(line,
"cpu")) {
327 if (!strstr(line,
"cpu ")) {
328 sscanf(line,
"cpu%d", &cpu);
330 ullreset(&idle2[cpu]);
333 p = strtok(line, tokens);
335 for (
int i=0; i<3; i++) {
336 p = strtok(NULL, tokens);
337 ullparse(&ulltmp, p);
341 ulladd(&ds2[cpu], &ulltmp);
344 p = strtok(NULL, tokens);
348 ullparse(&idle2[cpu], p);
364 unsigned long get_width(
long actif,
long idle,
long line_len)
371 j = (actif * 100) / j;
373 j = j * (double)(line_len / 100.0);
376 if (actif > 0 && j < 2)
380 j = (double)line_len;
382 return (
unsigned long) j;
396 static char *line = NULL;
397 static size_t line_size = 0;
400 fseek(fpstat, 0, SEEK_SET);
401 while ((getline(&line, &line_size, fpstat)) > 0) {
402 if (strstr(line,
"cpu") && !strstr(line,
"cpu "))
403 sscanf(line,
"cpu%d", &cpu);
419 printf(
"\n wmcpuwatch Copyright (C) 2017 Andreas Tscharner\n");
420 printf(
" This program comes with ABSOLUTELY NO WARRANTY;\n");
421 printf(
" This is free software, and you are welcome to\n");
422 printf(
" redistribute it under certain conditions;\n");
424 printf(
"Usage: %s [OPTION]...\n", name);
425 printf(
"WindowMaker dockapp that displays the cpu load of all CPUs.\n");
426 printf(
" -display DISPLAY contact the DISPLAY X server\n");
427 printf(
" -geometry GEOMETRY position the dockapp at GEOMETRY\n");
428 printf(
" -h display this help and exit\n");
429 printf(
" -v output version information and exit\n");
443 printf(
"wmcpuwatch version %s\n", PACKAGE_VERSION);
void clear_widgets(void)
Clear both widgets.
ullong * idle_last
Values for last idle cpu times.
ullong statlast
Field for the last value of the average cpu load.
void initialize_stat_device(stat_dev *, int, const char *)
Initialize stat_device struct.
Struct to keep CPU load data.
void update_stat_cpu(FILE *, stat_dev *, ullong *, ullong *, int)
Update CPU stats.
int main(int argc, char *argv[])
Main function.
long * idle_stat
Fields for all cpu idle times.
void print_version(void)
Definition of current version.
unsigned long get_width(long, long, long)
Get widht of active part.
int get_number_of_CPU(FILE *)
Returns the number of CPUs.
long rt_idle
Field for current average cpu idle time.
ullong idlelast
Field for the last average idle time value of the cpu.
long rt_stat
Field for current average cpu load.
void wmcpuwatch(int, char **)
Actual function for the dockapp.
ullong * cpu_last
Values for last cpu load.
void get_statistics(FILE *, ullong *, ullong *, ullong *, ullong *)
Parses /proc/stat and gets required values.
long * cpu_stat
Fields for all cpu loads.