Fixing file permissions
[dockapps.git] / wmcpufreq / wmcpufreq / wmcpufreq.c~
blob08f4583f10ed02a26bbbe38e46e72c9c0e4c5c88
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 "../wmgeneral/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    unsigned long freq[8];
65    XEvent event;
66    char gov[20];
67    char drv[20],*ptr;
68    char prg[LN_PATH];
69    ts.tv_sec=0;
70    ts.tv_nsec=DELAY;
71    prg[0]=0;
72    for(i=0;i<MAX_CPU;i++)
73      freq[i]=0;
74    if(argc >1)
75      {
76         for (i=1; i<=argc; i++) 
77           {
78              if (!strcmp(argv[i], "-v"))
79                {
80                   printf(WMCPUFREQ_VERSION);
81                   exit(0);
82                }
83              if (!strcmp(argv[i], "-exe"))
84                {
85                   if(strlen(argv[i+1]) < LN_PATH )
86                     strcpy(prg,argv[i+1]);
87                   break;
88                }
89           }
90      }
91    /* get driver name (guess all cpu have the same driver) */
92    ptr=cpufreq_get_driver(cpu);
93    if(!ptr)
94      {
95         printf("no driver found \n");
96         exit(-1);
97      }
98    strcpy(drv,ptr);
99    cpufreq_put_driver(ptr);
100    /* get number of cpu (0=cpu0, 1=cpu1 ...) */
101    ncpu=3;
102    if( cpufreq_cpu_exists(ncpu) ==0)
103      {
104         /* 4 cpu at least */
105         printf("4cpu\n");
106         wm_xpm=wmcpufreq_master_3;
107         wm_bits=wmcpufreq_mask_3_bits;
108      }
109    else if (cpufreq_cpu_exists(ncpu-1) ==0)
110      {
111         printf("3cpu\n");
112         wm_xpm=wmcpufreq_master_3;
113         wm_bits=wmcpufreq_mask_3_bits;
114         ncpu=2;
115      }
116    else if (cpufreq_cpu_exists(ncpu-2) ==0)
117    {
118       printf("2cpu\n");
119       wm_xpm=wmcpufreq_master_xpm_2;
120       wm_bits=wmcpufreq_mask_bits_2;
121       ncpu=1;
122    }
123    else
124    {
125       printf("1cpu\n");
126       wm_xpm=wmcpufreq_master_xpm_1;
127       wm_bits=wmcpufreq_mask_bits_1;
128       ncpu=0;
129    }
130    /* guess every cpu has the same limits */
131    if(cpufreq_get_hardware_limits(cpu, &f_min, &f_max))
132      {
133         printf("can't determine hardware limits \n");
134         exit(-1);
135      }
136    openXwindow(argc,argv,
137                wm_xpm,
138                (char*)wm_bits,
139                wmcpufreq_mask_width,
140                wmcpufreq_mask_height);   
141    while(1) 
142      { 
143         /* Process any pending X events */
144         while(XPending(display)) 
145           {
146              XNextEvent(display, &event);
147              switch(event.type)
148                {
149                 case Expose:
150                   RedrawWindow();
151                   break;
152                 case ButtonPress:
153                   if(strlen(prg))
154                     execCommand(prg);
155                   break;
156                 case ButtonRelease:
157                   break;
158                }
159           }
160         RedrawWindow();
161         /* get info */
162         for(i=0;i<=ncpu;i++)
163           freq[i]=cpufreq_get_freq_kernel(i);
164         policy=cpufreq_get_policy(cpu);
165         strcpy(gov,policy->governor);
166         max=policy->max;
167         min=policy->min;
168         cpufreq_put_policy(policy);
169         /* show info */
170         show_mhz(freq,ncpu);
171         if (ncpu==0)
172           show_driver(drv);
173         show_governor(gov);
174         /* delay */
175         nanosleep(&ts,&ts1);
176      }
178 void show_driver( char *pt)
180    int i,a,c;
181    for(i=0;i<8;i++)
182      {
183         a=(int)pt[i];
184         c=show_char(a);
185         copyXPMArea(6+(6*c),64,6,9, 7+(6*i), 34);
186      }  
187    RedrawWindow();
188    return;
190 void show_governor(char *ptr)
192    int i,a,c;
193    for(i=0;i<8;i++)
194      {
195         a=(int)ptr[i];
196         c=show_char(a);
197         copyXPMArea(6+(6*c),78,6,9, 7+(6*i), 49);
198      }  
199    RedrawWindow();
200    return;
202 void show_mhz(unsigned long *kHz,int nu) 
204    int i,j,delta=0,ddelta=0,odelta=0;
205    char buffer[5];
206    memset(buffer,0,5);
207    switch (nu) {
208     case 0:
209       snprintf(buffer, 5, "%4ld", (kHz[0] / 1000));
210       for (i=0;i<4;i++) 
211       {
212          if (buffer[i] == ' ') 
213          {
214             /* blank zero */
215             copyXPMArea(75, 93, 6, 9, 7+(7*i), 7);
216          }
217          else 
218          {
219             /* a standard digit */
220             copyXPMArea(((buffer[i]-48)*7)+5,93, 6, 9, 7+(7*i), 7);
221          }
222          /* update  speed bar */
223         copyXPMArea(65, 18, 49, 9, 7, 18);
224                  //          or x,y
225                  //         //                  l,h
226                  //         //                         dest x,y
227         copyXPMArea(65, 40, ((kHz[0] - f_min) / ((f_max - f_min) / 49)), 9, 7, 18);
228         RedrawWindow(); 
229       }
230       break;
231     case 1:
232       for(j=0;j<=nu;j++)
233       {
234          snprintf(buffer, 5, "%4ld", (kHz[j] / 1000));
235         
236          for (i=0;i<4;i++) 
237          {
238              if (buffer[i] == ' ') 
239              {
240                 /* blank zero */
241                 copyXPMArea(75, 93, 6, 9, 7+(7*i), 7+delta);
242              }
243              else 
244              {
245                 /* a standard digit */
246                 copyXPMArea(((buffer[i]-48)*7)+5, 93, 6, 9, 7+(7*i), 7+delta);
247              }
248           }
249        /* update  speed bar */
250        copyXPMArea(65, 18, 49, 4, 7, 18+delta);
251        copyXPMArea(65, 40, ((kHz[j] - f_min) / ((f_max - f_min) / 24)), 34, 7, 18+delta);
252              
253        RedrawWindow();
254        delta=19;
255      }
256      break;
257     case 2:
258       kHz[3]=0;
259     case 3:
260       odelta=14;
261       ddelta=10;
262       for(j=0;j<4;j++)
263       {
264          snprintf(buffer, 5, "%4ld", (kHz[j] / 1000));
265         
266          for (i=0;i<4;i++) 
267          {
268              if (buffer[i] == ' ') 
269              {
270                 /* blank zero */
271                 copyXPMArea(126,6+odelta*j ,6,7, 6+(6*i),5+ddelta*j);
272              }
273              else 
274              {
275                 /* a standard digit */
276                 copyXPMArea(((buffer[i]-48)*6)+66,6+odelta*j, 6,7, 6+(6*i), 5+ddelta*j);
277              }
278           }
279           /* clean sped bar */
280           copyXPMArea(31,67,24,7,34,5+ddelta*j);
281           /* update spped bar */
282          if( !(nu==2 && j==3) ) /* 3 cpu 4 bar nul */
283           copyXPMArea(6,67 , ((kHz[j] - f_min) / ((f_max - f_min) / 24)),7, 34,5+ddelta*j);            
284        RedrawWindow();
285       }
286       break;
287    }
288    return;
290 int show_char(int c)
292    switch (c)
293      {
294       case 0x61 ... 0x7a:
295         break;
296       case 0x41 ... 0x5a:
297         c=c+0x20;
298         break;
299       case 0x34:
300         c=(int)'z'+3;
301         break;
302       case 0x2d:
303         c='z'+4;
304         break;
305       default:
306         c='z'+1;
307      }
308    c=c-0x61;
309    return c;