1 /* Xwireless.c, status: experimental, do not distribute!! */
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>
18 #include <linux/wireless.h>
20 char* status[] = { "Scanning","Registering","Best AP","Good AP",
21 "Poor AP","Active Beacon Search","Static Load Balance",
24 typedef struct privateData {
35 struct iw_statistics stats;
36 struct iw_range range;
39 static XtAppContext app_context;
40 static Widget scrollbar;
41 static Widget topLevel;
43 static XtIntervalId timerId;
44 static privateData priv;
46 static int getstats(char *ifname, struct iw_statistics *stats)
49 FILE *f=fopen("/proc/net/wireless","r");
54 while(fgets(buf,255,f))
57 while(*bp&&isspace(*bp))
59 if( strncmp(bp,ifname,strlen(ifname))==0 && bp[strlen(ifname)]==':') {
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);
81 stats->qual.level = 0;
82 stats->qual.noise = 0;
87 /*strcpy(wrq.ifr_name, ifname);
88 wrq.u.data.pointer = (caddr_t) ⦥
89 wrq.u.data.length = 0;
91 if(ioctl(skfd, SIOCGIWRANGE, &wrq) >= 0) {
98 static void update( XtPointer client_data, XtIntervalId *id )
101 static int pixel = -1;
102 static int lpixel = -1;
103 static int bpixel = -1;
105 getstats( priv.ifname, &(priv.stats));
108 sprintf( buf, "%s", status[priv.stats.status] );
110 sprintf( buf, "%s", "buggy" );
111 XtVaSetValues( label, XtNlabel, buf, NULL );
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 );
128 if (pixel != priv.highColor )
129 XtVaSetValues( scrollbar,
130 XtNforeground, pixel = priv.highColor, NULL );
133 XawScrollbarSetThumb( scrollbar, 0.0, priv.stats.qual.qual / 255.0 );
135 timerId = XtAppAddTimeOut( app_context, 1000 , update, app_context );
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 },
158 int main( int argc, char **argv ) {
163 int fontWidth, fontHeight;
166 /* The device name must be the first argument */
170 priv.ifname = argv[1];
172 if( priv.ifname == (char *) NULL) {
173 printf("Usage: xwireless <interface>\n");
177 topLevel = XtVaAppInitialize( &app_context, "Xwireless",
179 &argc, argv, NULL, NULL );
181 XtGetApplicationResources( topLevel,
184 XtNumber( resources ),
187 priv.highValue = 170;
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 );*/
197 cursor = XCreateFontCursor( XtDisplay( topLevel ), XC_top_left_arrow );
199 form = XtVaCreateManagedWidget( "form",
200 formWidgetClass, topLevel,
201 XtNorientation, XtorientHorizontal,
203 XtNdefaultDistance, 2,
206 label = XtVaCreateManagedWidget( "label",
207 labelWidgetClass, form,
208 XtNleft, XtChainLeft,
209 XtNinternalHeight, 0,
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 );
220 scrollbar = XtVaCreateManagedWidget( "scrollbar",
221 scrollbarWidgetClass, form,
224 XtNorientation, XtorientHorizontal,
225 XtNscrollHCursor, cursor,
226 XtNthickness, fontHeight,
227 XtNlength, (width > fontWidth*4 - 6)
228 ? width - fontWidth * 4 - 6
232 XawScrollbarSetThumb( scrollbar, 0.0, 0.0 );
233 /* XtVaSetValues( scrollbar,
234 XtNtranslations, XtParseTranslationTable( "" ), NULL );
236 XtRealizeWidget( topLevel );
237 timerId = XtAppAddTimeOut( app_context, 0, update, app_context );
238 XtAppMainLoop( app_context );