wmbiff: EXAMINE before STATUS
[dockapps.git] / wmcpufreq / wmcpufreq / wmcpufreq.c
blob4be5d3d85d6be19e87ebc7db1b20dc743bffeee4
2 /*************************************************************************/
3 /* piece of code and pixmaps from */
4 /* */
5 /* wmspeedfreq ( Tom Kistner ) */
6 /* wmppp ( Martijn Pieterse, Antoine Nulle ) */
7 /* wmapkill (S.Rozange */
8 /* */
9 /* This program is free software; you can redistribute it and/or modify */
10 /* it under the terms of the GNU General Public License as published by */
11 /* the Free Software Foundation; either version 2, or (at your option) */
12 /* any later version. */
13 /* */
14 /* This program is distributed in the hope that it will be useful, */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17 /* GNU General Public License for more details. */
18 /* */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with this program (see the file COPYING); if not, write to the */
21 /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */
22 /* Boston, MA 02111-1307, USA */
23 /* */
24 /* you need libcpufreq for the libarary */
25 /* and libcpufreq-dev for cpufreq.h */
26 /* */
27 /* */
28 /*************************************************************************/
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <X11/X.h>
34 #include <X11/xpm.h>
35 #include <cpufreq.h>
36 #include <time.h>
38 #include <libdockapp/wmgeneral.h>
39 #include "wmcpufreq_master_1.xpm"
40 #include "wmcpufreq_master_2.xpm"
41 #include "wmcpufreq_master_3.xpm"
42 #include "wmcpufreq_mask_1.xbm"
43 #include "wmcpufreq_mask_2.xbm"
44 #include "wmcpufreq_mask_3.xbm"
46 #define DELAY 200000000 /*nano second */
47 #define WMCPUFREQ_VERSION "VERSION 3.0 2009/05/14 \n"
48 #define LN_PATH 40
49 #define MAX_CPU 4
50 pid_t execCommand(char *);
51 void show_mhz(unsigned long*,int);
52 void show_governor(char* );
53 void show_driver(char *);
54 int show_char(int);
55 static char **wm_xpm;
56 static unsigned char *wm_bits;
57 struct cpufreq_policy *policy;
58 int cpu=0;
59 unsigned long min,max,f_min,f_max;
60 int main(int argc, char *argv[])
62 struct timespec ts,ts1;
63 int i,ncpu;
64 int idcpu;
65 unsigned long freq[8];
66 XEvent event;
67 char gov[20];
68 char drv[20],*ptr,*endptr;
69 char prg[LN_PATH];
70 ts.tv_sec=0;
71 ts.tv_nsec=DELAY;
72 prg[0]=0;
73 idcpu=0;
75 for(i=0;i<MAX_CPU;i++)
76 freq[i]=0;
77 if(argc >1)
79 for (i=1; i<=argc; i++)
81 if (!strcmp(argv[i], "-v"))
83 printf(WMCPUFREQ_VERSION);
84 exit(0);
86 if (!strcmp(argv[i], "-exe"))
88 if(strlen(argv[i+1]) < LN_PATH )
89 strcpy(prg,argv[i+1]);
90 break;
92 if (!strcmp(argv[i], "-cpuid"))
94 if(strlen(argv[i+1]) < LN_PATH )
95 idcpu=strtol(argv[i+1],&endptr,0);
96 printf("cpuid= %d \n",idcpu);
97 break;
99 printf("only -v, -exe, -cpuid supported \n");
100 exit(0);
104 /* basic checks */
105 if ( idcpu < 0 )
107 printf("cpuid < 0 \n");
108 exit(-1);
111 /* get driver name (guess all cpu have the same driver) */
112 ptr=cpufreq_get_driver(cpu);
113 if(!ptr)
115 printf("no driver found \n");
116 exit(-1);
118 strcpy(drv,ptr);
119 cpufreq_put_driver(ptr);
122 /* get number of cpu (0=cpu0, 1=cpu1 ...) */
124 ncpu=-1;
126 for(i=0;i<MAX_CPU;i++)
128 if( cpufreq_cpu_exists(idcpu+i) ==0)
130 printf("cpuid %d found\n",idcpu+i);
131 ncpu=i;
135 switch ( ncpu ) {
136 case -1:
137 printf("no cpuid found \n");
138 exit(-1);
139 case 0:
140 wm_xpm=wmcpufreq_master_xpm_1;
141 wm_bits=wmcpufreq_mask_bits_1;
142 break;
143 case 1:
144 wm_xpm=wmcpufreq_master_xpm_2;
145 wm_bits=wmcpufreq_mask_bits_2;
146 break;
147 case 2:
148 wm_xpm=wmcpufreq_master_3;
149 wm_bits=wmcpufreq_mask_3_bits;
150 break;
151 case 3:
152 wm_xpm=wmcpufreq_master_3;
153 wm_bits=wmcpufreq_mask_3_bits;
154 break;
155 default:
156 printf("no yet implemented: cpuid %d \n",ncpu);
157 exit(-1);
158 break;
161 /* guess every cpu has the same limits */
162 if(cpufreq_get_hardware_limits(cpu, &f_min, &f_max))
164 printf("can't determine hardware limits \n");
165 exit(-1);
167 openXwindow(argc,argv,
168 wm_xpm,
169 (char*)wm_bits,
170 wmcpufreq_mask_width,
171 wmcpufreq_mask_height);
172 while(1)
174 /* Process any pending X events */
175 while(XPending(display))
177 XNextEvent(display, &event);
178 switch(event.type)
180 case Expose:
181 RedrawWindow();
182 break;
183 case ButtonPress:
184 if(strlen(prg))
185 execCommand(prg);
186 break;
187 case ButtonRelease:
188 break;
191 RedrawWindow();
192 /* get info */
193 for(i=0;i<=ncpu;i++)
194 freq[i]=cpufreq_get_freq_kernel(i+idcpu);
195 policy=cpufreq_get_policy(cpu);
196 strcpy(gov,policy->governor);
197 max=policy->max;
198 min=policy->min;
199 cpufreq_put_policy(policy);
200 /* show info */
201 show_mhz(freq,ncpu);
202 if (ncpu==0)
203 show_driver(drv);
204 show_governor(gov);
205 /* delay */
206 nanosleep(&ts,&ts1);
209 void show_driver( char *pt)
211 int i,a,c;
212 for(i=0;i<8;i++)
214 a=(int)pt[i];
215 c=show_char(a);
216 copyXPMArea(6+(6*c),64,6,9, 7+(6*i), 34);
218 RedrawWindow();
219 return;
221 void show_governor(char *ptr)
223 int i,a,c;
224 for(i=0;i<8;i++)
226 a=(int)ptr[i];
227 c=show_char(a);
228 copyXPMArea(6+(6*c),78,6,9, 7+(6*i), 49);
230 RedrawWindow();
231 return;
233 void show_mhz(unsigned long *kHz,int nu)
235 int i,j,delta=0,ddelta=0,odelta=0;
236 char buffer[5];
237 memset(buffer,0,5);
238 switch (nu) {
239 case 0:
240 snprintf(buffer, 5, "%4ld", (kHz[0] / 1000));
241 for (i=0;i<4;i++)
243 if (buffer[i] == ' ')
245 /* blank zero */
246 copyXPMArea(75, 93, 6, 9, 7+(7*i), 7);
248 else
250 /* a standard digit */
251 copyXPMArea(((buffer[i]-48)*7)+5,93, 6, 9, 7+(7*i), 7);
253 /* update speed bar */
254 copyXPMArea(65, 18, 49, 9, 7, 18);
255 // or x,y
256 // // l,h
257 // // dest x,y
258 copyXPMArea(65, 40, ((kHz[0] - f_min) / ((f_max - f_min) / 49)), 9, 7, 18);
259 RedrawWindow();
261 break;
262 case 1:
263 for(j=0;j<=nu;j++)
265 snprintf(buffer, 5, "%4ld", (kHz[j] / 1000));
267 for (i=0;i<4;i++)
269 if (buffer[i] == ' ')
271 /* blank zero */
272 copyXPMArea(75, 93, 6, 9, 7+(7*i), 7+delta);
274 else
276 /* a standard digit */
277 copyXPMArea(((buffer[i]-48)*7)+5, 93, 6, 9, 7+(7*i), 7+delta);
280 /* update speed bar */
281 copyXPMArea(65, 18, 49, 4, 7, 18+delta);
282 copyXPMArea(65, 40, ((kHz[j] - f_min) / ((f_max - f_min) / 49)), 4, 7, 18+delta);
284 RedrawWindow();
285 delta=19;
287 break;
288 case 2:
289 kHz[3]=0;
290 case 3:
291 odelta=14;
292 ddelta=10;
293 for(j=0;j<4;j++)
295 snprintf(buffer, 5, "%4ld", (kHz[j] / 1000));
297 for (i=0;i<4;i++)
299 if (buffer[i] == ' ')
301 /* blank zero */
302 copyXPMArea(126,6+odelta*j ,6,7, 6+(6*i),5+ddelta*j);
304 else
306 /* a standard digit */
307 copyXPMArea(((buffer[i]-48)*6)+66,6+odelta*j, 6,7, 6+(6*i), 5+ddelta*j);
310 /* clean sped bar */
311 copyXPMArea(31,67,24,7,34,5+ddelta*j);
312 /* update spped bar */
313 if( !(nu==2 && j==3) ) /* 3 cpu 4 bar nul */
314 copyXPMArea(6,67 , ((kHz[j] - f_min) / ((f_max - f_min) / 24)),7, 34,5+ddelta*j);
315 RedrawWindow();
317 break;
319 return;
321 int show_char(int c)
323 switch (c)
325 case 0x61 ... 0x7a:
326 break;
327 case 0x41 ... 0x5a:
328 c=c+0x20;
329 break;
330 case 0x34:
331 c=(int)'z'+3;
332 break;
333 case 0x2d:
334 c='z'+4;
335 break;
336 default:
337 c='z'+1;
339 c=c-0x61;
340 return c;