OSDN Git Service

v20
[android-x86/external-wireless-tools.git] / wireless_tools / xwireless.c
1 /* Xwireless.c, status: experimental, do not distribute!! */
2 #include <stdio.h>
3 #include <getopt.h>
4 #include <sys/types.h>
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <ctype.h>
10
11 #include <X11/Intrinsic.h>
12 #include <X11/StringDefs.h>
13 #include <X11/cursorfont.h>
14 #include <X11/Xaw/Form.h>
15 #include <X11/Xaw/Label.h>
16 #include <X11/Xaw/Scrollbar.h>
17
18 #include <linux/wireless.h>
19
20 char* status[] = { "Scanning","Registering","Best AP","Good AP",
21                           "Poor AP","Active Beacon Search","Static Load Balance",
22                           "Balance Search" };
23
24 typedef struct privateData {
25         char    *ifname;
26         Pixel   currentColor;
27         Pixel   highColor;
28         Pixel   lowColor;
29         Pixel   criticalColor;
30         Pixel   foreground;
31         int     highValue;
32         int     lowValue;
33         int     delay;
34         String  geometry;
35         struct  iw_statistics stats;
36         struct  iw_range range;
37 } privateData;
38
39 static XtAppContext          app_context;
40 static Widget                scrollbar;
41 static Widget                topLevel;
42 static Widget                label;
43 static XtIntervalId          timerId;
44 static privateData    priv;
45
46 static int getstats(char *ifname, struct iw_statistics *stats)
47 {
48         struct iwreq wrq;
49         FILE *f=fopen("/proc/net/wireless","r");
50         char buf[256];
51         char *bp;
52         if(f==NULL)
53                 return -1;
54         while(fgets(buf,255,f))
55         {
56                 bp=buf;
57                 while(*bp&&isspace(*bp))
58                         bp++;
59                 if( strncmp(bp,ifname,strlen(ifname))==0 && bp[strlen(ifname)]==':') {
60                         bp=strchr(bp,':');
61                         bp++;
62                         bp = strtok(bp, " .");
63                         sscanf(bp, "%X", &stats->status);
64                         bp = strtok(NULL, " .");
65                         sscanf(bp, "%d", &stats->qual.qual);
66                         bp = strtok(NULL, " .");
67                         sscanf(bp, "%d", &stats->qual.level);
68                         bp = strtok(NULL, " .");
69                         sscanf(bp, "%d", &stats->qual.noise);
70                         bp = strtok(NULL, " .");
71                         sscanf(bp, "%d", &stats->discard.nwid);
72                         bp = strtok(NULL, " .");
73                         sscanf(bp, "%d", &stats->discard.code);
74                         bp = strtok(NULL, " .");
75                         sscanf(bp, "%d", &stats->discard.misc);
76                         fclose(f);
77                         return 0;
78                 } else {
79                         stats->status = -1;
80                         stats->qual.qual = 0;
81                         stats->qual.level = 0;
82                         stats->qual.noise = 0;
83                 }
84         }
85         fclose(f);
86
87         /*strcpy(wrq.ifr_name, ifname);
88         wrq.u.data.pointer = (caddr_t) &range;
89         wrq.u.data.length = 0;
90         wrq.u.data.flags = 0;
91         if(ioctl(skfd, SIOCGIWRANGE, &wrq) >= 0) {
92                 info->has_range = 1;
93     }*/
94         
95         return 0;
96 }
97
98 static void update( XtPointer client_data, XtIntervalId *id )
99 {
100         char       buf[128];
101         static int pixel           = -1;
102         static int lpixel          = -1;
103         static int bpixel          = -1;
104
105         getstats( priv.ifname, &(priv.stats));
106
107         if(status < 8)
108           sprintf( buf, "%s", status[priv.stats.status] );
109         else
110           sprintf( buf, "%s", "buggy" );
111         XtVaSetValues( label, XtNlabel, buf, NULL );
112
113         if (priv.stats.qual.qual <= priv.lowValue) {
114                 if (pixel != priv.criticalColor)
115                         XtVaSetValues( scrollbar, XtNforeground,
116                                                    pixel = priv.criticalColor, NULL );
117                 if (bpixel != priv.criticalColor)
118                         XtVaSetValues( scrollbar, XtNborderColor,
119                                                    bpixel = priv.criticalColor, NULL );
120         } else if (priv.stats.qual.qual <= priv.highValue) {
121                 if (pixel != priv.lowColor)
122                         XtVaSetValues( scrollbar, 
123                                                    XtNforeground, pixel = priv.lowColor, NULL );
124                 if (bpixel != priv.foreground)
125                         XtVaSetValues( scrollbar, XtNborderColor,
126                                                    bpixel = priv.foreground, NULL );
127         } else {
128                 if (pixel != priv.highColor )
129                         XtVaSetValues( scrollbar, 
130                                                    XtNforeground, pixel = priv.highColor, NULL );
131         }
132         
133         XawScrollbarSetThumb( scrollbar, 0.0, priv.stats.qual.qual / 255.0 );
134  
135         timerId = XtAppAddTimeOut( app_context, 1000 , update, app_context );
136 }
137
138 #define offset(field) XtOffsetOf( privateData, field )
139 static XtResource resources[] = {
140         { "highColor", XtCForeground, XtRPixel, sizeof(Pixel),
141           offset(highColor), XtRString, "green" },
142         { "lowColor", XtCForeground, XtRPixel, sizeof(Pixel),
143           offset(lowColor), XtRString, "orange" },
144         { "criticalColor", XtCForeground, XtRPixel, sizeof(Pixel),
145           offset(criticalColor), XtRString, "red" },
146         { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
147           offset(foreground), XtRString, XtDefaultForeground },
148         { "highValue", XtCValue, XtRInt, sizeof(int),
149           offset(highValue), XtRImmediate, (XtPointer)50 },
150         { "lowValue", XtCValue, XtRInt, sizeof(int),
151           offset(lowValue), XtRImmediate, (XtPointer)10 },
152         { "geometry", XtCString, XtRString, sizeof( String ),
153           offset(geometry), XtRString, (XtPointer)"10x100" },
154         { "delay", XtCValue, XtRInt, sizeof(int),
155           offset(delay), XtRImmediate, (XtPointer)1 },
156 };
157
158 int main( int argc, char **argv ) {
159         Cursor           cursor;
160         int              c;
161         Widget           form;
162         XFontStruct      *fs;
163         int              fontWidth, fontHeight;
164         int              width = 120;
165         
166         /* The device name must be the first argument */
167         if(argc < 2) {
168                 printf("Hmm\n");                
169     }
170         priv.ifname = argv[1];
171
172         if( priv.ifname == (char *) NULL) {
173                 printf("Usage: xwireless <interface>\n");
174                 exit(-1);
175         }
176
177         topLevel = XtVaAppInitialize( &app_context, "Xwireless",
178                                                                   NULL, 0,
179                                                                   &argc, argv, NULL, NULL );
180
181         XtGetApplicationResources( topLevel,
182                                                            &priv,
183                                                            resources,
184                                                            XtNumber( resources ),
185                                                            NULL, 0 );
186         priv.lowValue = 85;
187         priv.highValue = 170;
188
189 /*      printf( "highColor = %ld\n",     priv.highColor );
190         printf( "lowColor = %ld\n",      priv.lowColor );
191         printf( "criticalColor = %ld\n", priv.criticalColor );
192         printf( "foreground = %ld\n",    priv.foreground );
193         printf( "highValue = %d\n",      priv.highValue );
194         printf( "lowValue = %d\n",       priv.lowValue );
195         printf( "geometry = %s\n",       priv.geometry );*/
196
197         cursor = XCreateFontCursor( XtDisplay( topLevel ), XC_top_left_arrow );
198         
199         form = XtVaCreateManagedWidget( "form",
200                                                                         formWidgetClass, topLevel,
201                                                                         XtNorientation, XtorientHorizontal,
202                                                                         XtNborderWidth, 0,
203                                                                         XtNdefaultDistance, 2,
204                                                                         NULL );
205    
206     label = XtVaCreateManagedWidget( "label",
207                                                                          labelWidgetClass, form,
208                                                                          XtNleft, XtChainLeft,
209                                                                          XtNinternalHeight, 0,
210                                                                          XtNinternalWidth, 0,
211                                                                          XtNborderWidth, 0,
212                                                                          XtNlabel, "Status",
213                                                                          NULL );
214         
215         XtVaGetValues( label, XtNfont, &fs, NULL );
216         fontWidth  = fs->max_bounds.width;
217         fontHeight = fs->max_bounds.ascent + fs->max_bounds.descent;
218         XtVaSetValues( label, XtNwidth, fontWidth * 8, NULL );
219         
220         scrollbar = XtVaCreateManagedWidget( "scrollbar",
221                                                                                  scrollbarWidgetClass, form,
222                                                                                  XtNhorizDistance, 3,
223                                                                                  XtNfromHoriz, label,
224                                                                                  XtNorientation, XtorientHorizontal,
225                                                                                  XtNscrollHCursor, cursor,
226                                                                                  XtNthickness, fontHeight,
227                                                                                  XtNlength, (width > fontWidth*4 - 6)
228                                                                                  ? width - fontWidth * 4 - 6
229                                                                                  : fontWidth * 4,
230                                                                                  NULL );
231         
232         XawScrollbarSetThumb( scrollbar, 0.0, 0.0 );
233 /*      XtVaSetValues( scrollbar,
234                                    XtNtranslations, XtParseTranslationTable( "" ), NULL );
235                                    */
236         XtRealizeWidget( topLevel );
237         timerId = XtAppAddTimeOut( app_context, 0, update, app_context );
238         XtAppMainLoop( app_context );
239         
240         return 0;
241 }