wmCalClock: bump to 1.26
[dockapps.git] / wmcore / wmcore.c
blobbb417f9ccf6eb13993d1073757152ca16c97a147
1 /*
2 wmcore by bitman <bitman@bitmania.de>
4 http://www.bitmania.de
6 based on wmlm by ben jarvis <bjarvis@bresnanlink.net>
8 This is a dockapp that shows the usage of each core in the system.
9 The dockapp splits into two displays, the upper one showing the common usage of the system and the
10 lower display showing one graph per each core.
12 It detects the number of cores and computes the usage to be represented as a bar graph.
13 wmcore works with a variable number of cores, I have tested the display with 1 up to 16 (simulated) cores.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include "config.h"
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <X11/Xlib.h>
40 #include <X11/xpm.h>
41 #include <X11/extensions/shape.h>
42 #include <libdockapp/wmgeneral.h>
43 #include <libdockapp/misc.h>
45 #include "wmcore_master.xpm"
46 #include "wmcore_mask.xbm"
51 #define DEBUG 0 // 0 disable, 1 enable
53 void usage() {
54 printf("Usage: wmcore [options]\n\n");
55 printf(" -d n Number of microseconds of delay between each screen update.\n");
56 printf(" (The default is 1000000 = 1 sec)\n");
60 #define MAX_CPU 20
64 int main(int argc, char **argv) {
65 short c;
66 int delay = 1000000;
67 unsigned int t;
68 unsigned char h;
69 unsigned int cpu; // current number of cpu when looping over all cpus
70 unsigned int cpu_max=0; // number of detected cpus in the system
72 unsigned int curr_user[MAX_CPU];
73 unsigned int curr_nice[MAX_CPU];
74 unsigned int curr_syst[MAX_CPU];
75 unsigned int curr_idle[MAX_CPU];
76 unsigned int curr_total[MAX_CPU];
78 unsigned int prev_idle[MAX_CPU];
79 unsigned int prev_total[MAX_CPU];
81 unsigned int diff_idle[MAX_CPU];
82 unsigned int diff_total[MAX_CPU];
83 unsigned int diff_usage[MAX_CPU];
86 char * token;
89 char tmp[200];
90 FILE *in;
92 XEvent Event;
94 fputs(PACKAGE_STRING " by bitman@bitmania.de\n", stdout);
95 for (c=1;c<=argc;c++) {
96 if (argv[c] == NULL) {}
97 else if (!strcmp(argv[c],"-d")) {
98 c++;
99 delay = atoi(argv[c]);
100 printf("delaying %d microseconds between updates\n", delay);
102 else if (!strcmp(argv[c],"-h")) {
103 usage();
104 exit(0);
108 #if DEBUG == 1
109 #endif
111 openXwindow(argc,argv,wmcore_master_xpm,wmcore_mask_bits,wmcore_mask_width,wmcore_mask_height);
112 RedrawWindow();
115 // Check how many cpu cores are present
116 if((in = fopen("/proc/stat","r")) == NULL)
118 printf("\nUnable to open file /proc/stat\n");
119 exit(1);
120 } else {
121 fgets(tmp,200,in);
122 while( strstr(tmp, "cpu") ) {
123 fgets(tmp,200,in);
124 cpu_max++;
126 cpu_max--;
127 fclose(in);
129 printf("CPU-Cores detected: %i\n",cpu_max);
132 // Calculate the height per bar, to fit in the screen
133 h=44/cpu_max;
136 // Init values
137 for(cpu=0; cpu<cpu_max; cpu++) {
138 prev_idle[cpu]=0;
139 prev_total[cpu]=0;
143 // the main loop
144 while (1) {
145 while(XPending(display)) {
146 XNextEvent(display,&Event);
147 switch (Event.type) {
148 case Expose:
149 RedrawWindow();
150 break;
151 case DestroyNotify:
152 XCloseDisplay(display);
153 exit(0);
154 break;
159 // get the cpu core usage
160 if((in = fopen("/proc/stat","r")) == NULL)
162 printf("\nUnable to open file /proc/stat\n");
163 exit(1);
164 } else {
165 fgets(tmp,200,in);
166 for(cpu=0; cpu < cpu_max; cpu++) {
167 fgets(tmp,200,in); // read next line
168 token = strtok(tmp," ");
169 curr_user[cpu] = atoi(strtok(NULL," "));
170 curr_nice[cpu] = atoi(strtok(NULL," "));
171 curr_syst[cpu] = atoi(strtok(NULL," "));
172 curr_idle[cpu] = atoi(strtok(NULL," "));
173 curr_total[cpu] = curr_user[cpu] + curr_nice[cpu] + curr_syst[cpu] + curr_idle[cpu];
174 diff_idle[cpu] = curr_idle[cpu]-prev_idle[cpu];
175 diff_total[cpu] = curr_total[cpu]-prev_total[cpu];
176 if(diff_total[cpu]>0){
177 diff_usage[cpu] = (unsigned int) (1000*(diff_total[cpu]-diff_idle[cpu])/diff_total[cpu]+5)/10 ;
178 } else {
179 diff_usage[cpu] = 0;
182 fclose(in);
185 // Display total usage over all cores in upper display field
186 t=0;
187 for(cpu=0; cpu<cpu_max; cpu++) {
188 t=t+diff_usage[cpu];
190 t=t/cpu_max;
191 t=((t*60)/100);
192 copyXPMArea(129,0, 61,8, 1, 2 ); // blank preveous bar
193 copyXPMArea(65,0, t,8, 1, 2 ); // draw new bar
197 // Display the bar graphs for each core in lower display field
198 for(cpu=0; cpu<cpu_max; cpu++) {
199 // erase preveous bar
200 copyXPMArea(129,0, 61,h, 1, 18+(cpu*h));
202 // calculate the length of the bar and display
203 t=((diff_usage[cpu]*60)/100);
204 copyXPMArea(65,0, t,h, 1, 18+(cpu*h));
206 // Remember the total and idle CPU times for the next run.
207 prev_total[cpu]=curr_total[cpu];
208 prev_idle[cpu]=curr_idle[cpu];
211 RedrawWindow();
212 usleep(delay);
214 return 0;