1 /* $Id: wmwlmon.c,v 1.41 2008/05/13 09:26:22 hacki Exp $ */
4 * Copyright (c) 2005, 2006 Marcus Glocker <marcus@nazgul.ch>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 #include "bitmaps/wmwlmon_mask.xbm"
34 #include "bitmaps/wmwlmon_master.xpm"
46 int main(int, char **);
48 void signal_handler(const int);
49 void debugloop(char *);
50 void draw_nwid(char *, int);
51 void draw_string(const char *, const int, const int);
52 void draw_signal(const int, const int);
53 int scroll_lcd(const int, const int, const int, const int, const int,
55 int scroll_bounce(const int, const int, const int, const int, const int,
57 int scroll_fade(const int, const int, const int, const int, const int,
61 * global variables for this file
63 static const char *version
= "1.0";
64 volatile sig_atomic_t quit
= 0;
65 char TimeColor
[30] = "#ffff00";
66 char BackgroundColor
[30] = "#181818";
74 { 60, 102, 6, 8 }, /* Dec 0 nul NOTUSED */
75 { 60, 102, 6, 8 }, /* Dec 1 soh NOTUSED */
76 { 60, 102, 6, 8 }, /* Dec 2 stx NOTUSED */
77 { 60, 102, 6, 8 }, /* Dec 3 etx NOTUSED */
78 { 60, 102, 6, 8 }, /* Dec 4 eot NOTUSED */
79 { 60, 102, 6, 8 }, /* Dec 5 enq NOTUSED */
80 { 60, 102, 6, 8 }, /* Dec 6 ack NOTUSED */
81 { 60, 102, 6, 8 }, /* Dec 7 bel NOTUSED */
82 { 60, 102, 6, 8 }, /* Dec 8 bs NOTUSED */
83 { 60, 102, 6, 8 }, /* Dec 9 ht NOTUSED */
84 { 60, 102, 6, 8 }, /* Dec 10 nl NOTUSED */
85 { 60, 102, 6, 8 }, /* Dec 11 vt NOTUSED */
86 { 60, 102, 6, 8 }, /* Dec 12 np NOTUSED */
87 { 60, 102, 6, 8 }, /* Dec 13 cr NOTUSED */
88 { 60, 102, 6, 8 }, /* Dec 14 so NOTUSED */
89 { 60, 102, 6, 8 }, /* Dec 15 si NOTUSED */
90 { 60, 102, 6, 8 }, /* Dec 16 dle NOTUSED */
91 { 60, 102, 6, 8 }, /* Dec 17 dc1 NOTUSED */
92 { 60, 102, 6, 8 }, /* Dec 18 dc2 NOTUSED */
93 { 60, 102, 6, 8 }, /* Dec 19 dc3 NOTUSED */
94 { 60, 102, 6, 8 }, /* Dec 20 dc4 NOTUSED */
95 { 60, 102, 6, 8 }, /* Dec 21 nak NOTUSED */
96 { 60, 102, 6, 8 }, /* Dec 22 syn NOTUSED */
97 { 60, 102, 6, 8 }, /* Dec 23 etb NOTUSED */
98 { 60, 102, 6, 8 }, /* Dec 24 can NOTUSED */
99 { 60, 102, 6, 8 }, /* Dec 25 em NOTUSED */
100 { 60, 102, 6, 8 }, /* Dec 26 sub NOTUSED */
101 { 60, 102, 6, 8 }, /* Dec 27 esc NOTUSED */
102 { 60, 102, 6, 8 }, /* Dec 28 fs NOTUSED */
103 { 60, 102, 6, 8 }, /* Dec 29 gs NOTUSED */
104 { 60, 102, 6, 8 }, /* Dec 30 rs NOTUSED */
105 { 60, 102, 6, 8 }, /* Dec 31 us NOTUSED */
106 { 60, 102, 6, 8 }, /* Dec 32 sp NOTUSED */
107 { 66, 102, 6, 8 }, /* Dec 33 '!' */
108 { 72, 102, 6, 8 }, /* Dec 34 '"' */
109 { 78, 102, 6, 8 }, /* Dec 35 '#' */
110 { 60, 102, 6, 8 }, /* Dec 36 '$' NOTUSED */
111 { 90, 101, 6, 8 }, /* Dec 37 '%' */
112 { 96, 102, 6, 8 }, /* Dec 38 '&' */
113 { 102, 102, 6, 8 }, /* Dec 39 ''' */
114 { 108, 102, 6, 8 }, /* Dec 40 '(' */
115 { 114, 102, 6, 8 }, /* Dec 41 ')' */
116 { 120, 102, 6, 8 }, /* Dec 42 '*' */
117 { 126, 102, 6, 8 }, /* Dec 43 '+' */
118 { 132, 102, 6, 8 }, /* Dec 44 ',' */
119 { 138, 102, 6, 8 }, /* Dec 45 '-' */
120 { 144, 102, 6, 8 }, /* Dec 46 '.' */
121 { 150, 102, 6, 8 }, /* Dec 47 '/' */
122 { 0, 102, 6, 6 }, /* Dec 48 '0' */
123 { 6, 102, 6, 6 }, /* Dec 49 '1' */
124 { 12, 102, 6, 6 }, /* Dec 50 '2' */
125 { 18, 102, 6, 6 }, /* Dec 51 '3' */
126 { 24, 102, 6, 6 }, /* Dec 52 '4' */
127 { 30, 102, 6, 6 }, /* Dec 53 '5' */
128 { 36, 102, 6, 6 }, /* Dec 54 '6' */
129 { 42, 102, 6, 6 }, /* Dec 55 '7' */
130 { 48, 102, 6, 6 }, /* Dec 56 '8' */
131 { 54, 102, 6, 6 }, /* Dec 57 '9' */
132 { 0, 112, 6, 8 }, /* Dec 58 ':' */
133 { 6, 113, 6, 8 }, /* Dec 59 ';' */
134 { 12, 112, 6, 8 }, /* Dec 60 '<'*/
135 { 18, 112, 6, 8 }, /* Dec 61 '='*/
136 { 24, 112, 6, 8 }, /* Dec 62 '>'*/
137 { 30, 112, 6, 8 }, /* Dec 63 '?'*/
138 { 36, 112, 6, 8 }, /* Dec 64 '@'*/
139 { 0, 79, 6, 8 }, /* Dec 65 'A' */
140 { 6, 79, 6, 8 }, /* Dec 66 'B' */
141 { 12, 79, 6, 8 }, /* Dec 67 'C' */
142 { 18, 79, 6, 8 }, /* Dec 68 'D' */
143 { 24, 79, 6, 8 }, /* Dec 69 'E' */
144 { 30, 79, 6, 8 }, /* Dec 70 'F' */
145 { 36, 79, 6, 8 }, /* Dec 71 'G' */
146 { 42, 79, 6, 8 }, /* Dec 72 'H' */
147 { 48, 79, 6, 8 }, /* Dec 73 'I' */
148 { 54, 79, 6, 8 }, /* Dec 74 'J' */
149 { 60, 79, 6, 8 }, /* Dec 75 'K' */
150 { 66, 79, 6, 8 }, /* Dec 76 'L' */
151 { 72, 79, 6, 8 }, /* Dec 77 'M' */
152 { 78, 79, 6, 8 }, /* Dec 78 'N' */
153 { 84, 79, 6, 8 }, /* Dec 79 'O' */
154 { 90, 79, 6, 8 }, /* Dec 80 'P' */
155 { 96, 79, 6, 8 }, /* Dec 81 'Q' */
156 { 102, 79, 6, 8 }, /* Dec 82 'R' */
157 { 108, 79, 6, 8 }, /* Dec 83 'S' */
158 { 114, 79, 6, 8 }, /* Dec 84 'T' */
159 { 120, 79, 6, 8 }, /* Dec 85 'U' */
160 { 126, 79, 6, 8 }, /* Dec 86 'V' */
161 { 132, 79, 6, 8 }, /* Dec 87 'W' */
162 { 138, 79, 6, 8 }, /* Dec 88 'X' */
163 { 144, 79, 6, 8 }, /* Dec 89 'Y' */
164 { 150, 79, 6, 8 }, /* Dec 90 'Z' */
165 { 42, 112, 6, 8 }, /* Dec 91 '[' */
166 { 48, 112, 6, 8 }, /* Dec 92 '\' */
167 { 54, 112, 6, 8 }, /* Dec 93 ']' */
168 { 60, 112, 6, 8 }, /* Dec 94 '^' */
169 { 66, 113, 6, 8 }, /* Dec 95 '_' */
170 { 72, 112, 6, 8 }, /* Dec 96 '`' */
171 { 0, 90, 6, 8 }, /* Dec 97 'a' */
172 { 6, 90, 6, 8 }, /* Dec 98 'b' */
173 { 12, 90, 6, 8 }, /* Dec 99 'c' */
174 { 18, 90, 6, 8 }, /* Dec 100 'd' */
175 { 24, 90, 6, 8 }, /* Dec 101 'e' */
176 { 30, 90, 6, 8 }, /* Dec 102 'f' */
177 { 36, 90, 6, 8 }, /* Dec 103 'g' */
178 { 42, 90, 6, 8 }, /* Dec 104 'h' */
179 { 48, 90, 6, 8 }, /* Dec 105 'i' */
180 { 54, 90, 6, 8 }, /* Dec 106 'j' */
181 { 60, 90, 6, 8 }, /* Dec 107 'k' */
182 { 66, 90, 6, 8 }, /* Dec 108 'l' */
183 { 72, 90, 6, 8 }, /* Dec 109 'm' */
184 { 78, 90, 6, 8 }, /* Dec 110 'n' */
185 { 84, 90, 6, 8 }, /* Dec 111 'o' */
186 { 90, 90, 6, 8 }, /* Dec 112 'p' */
187 { 96, 90, 6, 8 }, /* Dec 113 'q' */
188 { 102, 90, 6, 8 }, /* Dec 114 'r' */
189 { 108, 90, 6, 8 }, /* Dec 115 's' */
190 { 114, 90, 6, 8 }, /* Dec 116 't' */
191 { 120, 90, 6, 8 }, /* Dec 117 'u' */
192 { 126, 90, 6, 8 }, /* Dec 118 'v' */
193 { 132, 90, 6, 8 }, /* Dec 119 'w' */
194 { 138, 90, 6, 8 }, /* Dec 120 'x' */
195 { 144, 90, 6, 8 }, /* Dec 121 'y' */
196 { 150, 90, 6, 8 }, /* Dec 122 'z' */
197 { 78, 112, 6, 8 }, /* Dec 123 '{' */
198 { 84, 112, 6, 8 }, /* Dec 124 '|' */
199 { 90, 112, 6, 8 }, /* Dec 125 '}' */
200 { 60, 112, 6, 8 }, /* Dec 126 '~' */
201 { 60, 102, 6, 8 } /* Dec 127 del NOTUSED */
210 extern char *__progname
;
213 fprintf(stderr
, "%s %s\n", __progname
, version
);
217 fprintf(stderr
, "usage: %s ", __progname
);
218 fprintf(stderr
, "[-Dhvw] [-i interface] [-s scrolling] ");
219 fprintf(stderr
, "[-display display]\n\n");
220 fprintf(stderr
, "options:\n");
221 fprintf(stderr
, " -D\t\t: Debug mode.\n");
222 fprintf(stderr
, " -h\t\t: This help.\n");
223 fprintf(stderr
, " -v\t\t: Shows version.\n");
224 fprintf(stderr
, " -w\t\t: Enable WaveLAN compatibility.\n");
225 fprintf(stderr
, " -i\t\t: Set interface.\n");
226 fprintf(stderr
, " -s\t\t: Network ID scrolling.\n");
227 fprintf(stderr
, " \t\t 0 = Disable 1 = LCD (default) 2 = Bounce ");
228 fprintf(stderr
, "3 = Fade\n");
229 fprintf(stderr
, " -display\t: Set display name.\n");
238 signal_handler(const int sig
)
269 * wireless monitor wmdockapp
274 main(int argc
, char *argv
[])
276 int i
, r
, ch
, opt_wavelan
= 0;
277 int opt_debug
= 0, nic_status
= 1, opt_scroll
= 1;
278 char *opt_interface
= NULL
, *opt_display
= NULL
;
279 char *nwid
= NULL
, *speed
= NULL
, *status
= NULL
;
287 * get command line options
290 /* keep the old good -display tradition */
291 for (i
= 0; i
< argc
; i
++) {
292 if (!strcmp(argv
[i
], "-display"))
296 while ((ch
= getopt(argc
, argv
, "Dd:hi:s:vw")) != -1) {
302 opt_display
= strdup(optarg
);
305 opt_interface
= strdup(optarg
);
309 opt_scroll
= strtonum(optarg
, 0, 3, &errstr
);
313 opt_scroll
= strtol(optarg
, 0, 10);
333 if (opt_interface
== NULL
) {
334 /* scan for interface */
335 opt_interface
= get_first_wnic();
336 if (opt_interface
== NULL
) {
337 fprintf(stderr
, "No wireless interface found\n");
341 /* check defined interface */
342 if (!check_nic(opt_interface
)) {
343 fprintf(stderr
, "%s: no such interface\n",
347 if (get_wep(opt_interface
) == -1) {
348 fprintf(stderr
, "%s: not a wireless ",
350 fprintf(stderr
, "interface\n");
355 /* is the interface wi? */
356 if ((strncmp(opt_interface
, "wi", 2) == 0) && isdigit(opt_interface
[2]))
360 * install signal handler
362 signal(SIGINT
, signal_handler
);
363 signal(SIGTERM
, signal_handler
);
364 signal(SIGCHLD
, signal_handler
);
365 signal(SIGHUP
, signal_handler
);
366 signal(SIGQUIT
, signal_handler
);
367 signal(SIGALRM
, signal_handler
);
368 signal(SIGPIPE
, signal_handler
);
374 debugloop(opt_interface
); /* does not return */
379 initXwindow(opt_display
);
384 openXwindow(argc
, argv
, wmwlmon_master
, wmwlmon_mask_bits
,
385 wmwlmon_mask_width
, wmwlmon_mask_height
);
390 for (i
= 100; !quit
; i
++) {
392 * get wirless network state and draw it
397 copyXPMArea(65, 0, 64, 64, 0, 0);
399 /* check if interface still exists */
400 nic_status
= check_nic(opt_interface
);
405 draw_string(opt_interface
, 6, 7);
408 status
= get_status(opt_interface
);
409 if (!strcmp(status
, "active"))
410 copyXPMArea(0, 67, 4, 4, 53, 8);
413 nwid
= get_nwid(opt_interface
);
416 speed
= get_speed(opt_interface
);
418 copyXPMArea(28, 67, 23, 8, 29, 36);
419 if (strlen(speed
) == 1)
420 draw_string(speed
, 18, 36);
421 if (strlen(speed
) == 2)
422 draw_string(speed
, 12, 36);
423 if (strlen(speed
) == 3)
424 draw_string(speed
, 6, 36);
427 /* signal strength */
429 r
= get_wi_signal(opt_interface
);
431 r
= get_signal(opt_interface
, nwid
);
434 copyXPMArea(15, 67, 10, 6, 30, 29);
435 snprintf(sig
, sizeof(sig
), "%d", r
);
436 if (strlen(sig
) == 1)
437 draw_string(sig
, 18, 29);
438 if (strlen(sig
) == 2)
439 draw_string(sig
, 12, 29);
440 if (strlen(sig
) == 3)
441 draw_string(sig
, 6, 29);
443 draw_signal(atoi(speed
), r
);
447 r
= get_channel(opt_interface
);
449 copyXPMArea(61, 67, 10, 6, 30, 44);
450 snprintf(chn
, sizeof(sig
), "%d", r
);
451 if (strlen(chn
) == 1)
452 draw_string(chn
, 18, 44);
453 if (strlen(chn
) == 2)
454 draw_string(chn
, 12, 44);
455 if (strlen(chn
) == 3)
456 draw_string(chn
, 6, 44);
460 r
= get_wep(opt_interface
);
462 copyXPMArea(54, 67, 4, 6, 48, 44);
465 if (nwid
!= NULL
&& i
% 2 == 0 && nic_status
) {
466 /* reset nwid area */
467 copyXPMArea(70, 50, 54, 9, 5, 50);
468 draw_nwid(nwid
, opt_scroll
);
472 * process pending X events
474 while (XPending(display
)) {
475 XNextEvent(display
, &event
);
494 debugloop(char *interface
)
496 char *status
, *nwid
, *speed
;
503 /* check if interface still exists */
504 if (!check_nic(interface
)) {
505 printf("NIC gone!\n");
511 printf("NIC:\t %s\n", interface
);
514 status
= get_status(interface
);
516 printf("Status:\t %s\n", status
);
519 nwid
= get_nwid(interface
);
521 printf("NwID:\t %s\n", nwid
);
524 speed
= get_speed(interface
);
526 printf("Speed:\t %s Mbps\n", speed
);
528 /* signal strength */
529 r
= get_signal(interface
, nwid
);
531 printf("Signal:\t %d dB\n", r
);
534 r
= get_channel(interface
);
536 printf("Channel: %d\n", r
);
539 r
= get_wep(interface
);
541 printf("WEP:\t enabled\n");
543 printf("WEP:\t disabled\n");
553 draw_nwid(char *nwid
, int scroll
)
555 static int offset
= 0;
556 static char *save_nwid
;
557 char network_id_cut
[NWIDLENC
+ 1];
559 if (strlen(nwid
) > NWIDLENC
) {
560 if (save_nwid
== NULL
|| strcmp(nwid
, save_nwid
)) {
562 if ((save_nwid
= strdup(nwid
)) == NULL
)
569 strlcpy(network_id_cut
, nwid
, sizeof(network_id_cut
));
570 draw_string(network_id_cut
, 6, 51);
574 offset
= scroll_lcd(0, 124, 6, 51, offset
, nwid
);
578 offset
= scroll_bounce(0, 124, 6, 51, offset
, nwid
);
582 offset
= scroll_fade(0, 124, 6, 51, offset
, nwid
);
586 draw_string(nwid
, 6, 51);
593 draw_string(const char *string
, const int x
, const int y
)
597 for (i
= 0, offset
= x
; string
[i
] != '\0'; i
++) {
600 copyXPMArea(fonts
[c
].x
, fonts
[c
].y
, fonts
[c
].w
, fonts
[c
].h
,
602 offset
+= fonts
[c
].w
;
607 * calculate the signal strength in percents and draw it
610 draw_signal(const int speed
, const int signal
)
612 int i
, bars
, loop
, offset
;
617 have
= (signal
* bars
) / need
;
623 for (i
= 0, offset
= 6; i
< loop
; i
++, offset
+= 2)
624 copyXPMArea(10, 67, 1, 6, offset
, 21);
629 * scrolls a string from left to right separated by a space
634 scroll_lcd(const int src_x
, const int src_y
, const int dst_x
,
635 const int dst_y
, const int offset
, const char *nwid
)
642 /* draw network id */
644 copyXPMArea(0, 132, 160, 8, src_x
, src_y
);
645 draw_string(nwid
, src_x
, src_y
);
648 /* calculate image length */
649 len
= (strlen(nwid
) + 1) * 6;
651 /* reached end of source, reset */
655 /* copy scroll frame to destination */
657 if (frame
>= NWIDLENP
)
658 copyXPMArea(pos
, src_y
, NWIDLENP
, 8, dst_x
, dst_y
);
660 copyXPMArea(pos
, src_y
, frame
, 8, dst_x
, dst_y
);
661 copyXPMArea(src_x
, src_y
, NWIDLENP
- frame
, 8, dst_x
+ frame
,
665 /* move to next pixel */
673 * scrolls a string from left to right and bounces back from right to left
678 scroll_bounce(const int src_x
, const int src_y
, const int dst_x
,
679 const int dst_y
, const int offset
, const char *nwid
)
682 static int delay
= 0, direction
= 0;
687 /* draw network id */
689 copyXPMArea(0, 132, 160, 8, src_x
, src_y
);
690 draw_string(nwid
, src_x
, src_y
);
693 /* calculate image length */
694 len
= strlen(nwid
) * 6;
699 copyXPMArea(pos
- 1, src_y
, NWIDLENP
, 8, dst_x
, dst_y
);
701 copyXPMArea(pos
+ 1, src_y
, NWIDLENP
, 8, dst_x
, dst_y
);
714 if (frame
== NWIDLENP
) {
719 /* copy scroll frame to destination */
720 copyXPMArea(pos
, src_y
, NWIDLENP
, 8, dst_x
, dst_y
);
722 /* move to next pixel */
733 * scrolls a string from left to right, fading it out and begins again
738 scroll_fade(const int src_x
, const int src_y
, const int dst_x
,
739 const int dst_y
, const int offset
, const char *nwid
)
742 static int delay
= 0;
747 /* draw network id */
749 copyXPMArea(0, 132, 160, 8, src_x
, src_y
);
750 draw_string(nwid
, src_x
, src_y
);
753 /* calculate image length */
754 len
= (strlen(nwid
) + 1) * 6;
758 copyXPMArea(pos
- 1, src_y
, NWIDLENP
, 8, dst_x
, dst_y
);
771 /* copy scroll frame to destination */
773 if (frame
>= NWIDLENP
)
774 copyXPMArea(pos
, src_y
, NWIDLENP
, 8, dst_x
, dst_y
);
776 copyXPMArea(pos
, src_y
, frame
, 8, dst_x
, dst_y
);
778 /* move to next pixel */