3 YAWMPPP - PPP dock app/helper for WindowMaker
4 Copyright (C) 2000,2001:
6 Author: Felipe Bergo (bergo@seul.org)
8 based on the wmppp application by
10 Martijn Pieterse (pieterse@xs4all.nl)
11 Antoine Nulle (warp@xs4all.nl)
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 $Id: yawmppp.c,v 1.1.1.1 2001/02/22 07:16:01 bergo Exp $
41 #include <sys/param.h>
42 #include <sys/types.h>
46 #include <X11/extensions/shape.h>
48 #include "wmgeneral.h"
53 #include "dockxpm.xpm"
58 extern char **environ
;
60 /* global variables */
65 char *active_interface
= "ppp0";
66 int TimerDivisor
= 60;
69 int dock_mask_width
= 64;
70 int dock_mask_height
= 64;
71 char dock_mask_bits
[64 * 64];
79 struct YAWMPPP_ISP_INFO IspData
[MAX_ISPS
];
85 int status
; /* 0=ok 1=error 2=crash */
98 #define PPP_STATS_HIS 54
102 int ppp_history
[PPP_STATS_HIS
+ 1][2];
107 main (int argc
, char *argv
[])
112 /* Parse Command Line */
115 if (strlen (ProgName
) >= 5)
116 ProgName
+= (strlen (ProgName
) - 5);
118 for (i
= 1; i
< argc
; i
++)
130 if (strcmp (arg
+ 1, "display"))
137 if (strcmp (arg
+ 1, "geometry"))
149 if (strncmp (argv
[i
+ 1], "ppp", 3))
154 active_interface
= argv
[i
+ 1];
170 updaterate
= atoi (argv
[i
]);
171 if (updaterate
< 1 || updaterate
> 10)
182 printf("Caution level: %d\n",caution
);
192 for (i
= 0; i
< MAX_ISPS
; i
++)
193 memset(&IspData
[i
],0,sizeof(struct YAWMPPP_ISP_INFO
));
196 signal(SIGUSR1
,sigusr_handler
);
197 signal(SIGHUP
,sigusr_handler
);
198 signal(SIGINT
,sigusr_handler
);
199 signal(SIGTERM
,sigusr_handler
);
204 yawmppp_routine (argc
, argv
);
210 yawmppp_routine (int argc
, char **argv
)
223 long ppp_send
, ppp_sl
= -1;
224 long ppp_recv
, ppp_rl
= -1;
225 long ppp_sbytes
, ppp_rbytes
;
226 long ppp_osbytes
, ppp_orbytes
;
230 pid_t stop_child
= 0;
231 pid_t start_child
= 0;
240 /* Initialize some stuff */
242 get_statistics (active_interface
, &ppp_rl
, &ppp_sl
,
243 &ppp_orbytes
, &ppp_osbytes
);
249 /* Open the display */
251 createXBMfromXPM (dock_mask_bits
, dockxpm_xpm
,
252 dock_mask_width
, dock_mask_height
);
254 openXwindow (argc
, argv
, dockxpm_xpm
, dock_mask_bits
,
255 dock_mask_width
, dock_mask_height
);
258 AddMouseRegion (0, 35, 48, 46, 58);
260 AddMouseRegion (1, 47, 48, 58, 58);
263 AddMouseRegion (2, 5, 48, 16, 58);
265 AddMouseRegion (3, 17, 48, 28, 58);
267 AddMouseRegion (4, 5, 6, 59, 43);
270 currenttime
= time (0);
276 /* 888k8 on bottom */
277 copyXPMArea (ERR_SRC_X
+28, ERR_SRC_Y
+9, 25, 8, ERR_DEST_X
, ERR_DEST_Y
);
283 lasttime
= currenttime
;
284 currenttime
= time (0);
286 /* Check if any child has left the playground */
287 i
= waitpid (0, &status
, WNOHANG
);
288 if (i
== stop_child
&& stop_child
!= 0)
292 SetOffLED (LED_PPP_POWER
);
293 SetOffLED (LED_PPP_RX
);
294 SetOffLED (LED_PPP_TX
);
295 /* 888k8 on bottom */
296 copyXPMArea (ERR_SRC_X
+28, ERR_SRC_Y
+9, 25, 8,
297 ERR_DEST_X
, ERR_DEST_Y
);
302 if (i
== start_child
&& start_child
!= 0)
304 if (WIFEXITED (status
))
306 if (WEXITSTATUS (status
) == 10)
311 copyXPMArea (ERR_SRC_X
+28, ERR_SRC_Y
+9, 25, 8,
312 ERR_DEST_X
, ERR_DEST_Y
);
313 SetOffLED (LED_PPP_POWER
);
321 /* On-line detectie! 1x per second */
323 if (currenttime
!= lasttime
)
327 if (stillonline (active_interface
))
332 starttime
= currenttime
;
334 if (stat (STAMP_FILE
, &st
) == 0)
335 starttime
= st
.st_mtime
;
337 SetOnLED (LED_PPP_POWER
);
341 copyXPMArea (ERR_SRC_X
+28, ERR_SRC_Y
+9, 25, 8,
342 ERR_DEST_X
, ERR_DEST_Y
);
344 if (IspData
[current_isp
].SpeedAction
)
345 DrawSpeedInd (IspData
[current_isp
].SpeedAction
);
347 speed_ind
= currenttime
+ 10;
355 SetErrLED (LED_PPP_POWER
);
359 copyXPMArea (ERR_SRC_X
, ERR_SRC_Y
+9, 25, 8,
360 ERR_DEST_X
, ERR_DEST_Y
);
362 if (IspData
[current_isp
].IfDownAction
)
363 execCommand (IspData
[current_isp
].IfDownAction
);
369 if (waittime
&& waittime
<= currenttime
)
371 SetOffLED (LED_PPP_POWER
);
376 if ((starttime
)&&(!isonline
)) {
379 logconn
.start
=time(NULL
);
381 strcpy(logconn
.longname
,IspData
[current_isp
].LongName
);
382 strcpy(logconn
.shortname
,IspData
[current_isp
].ShortName
);
383 strcpy(logconn
.user
,IspData
[current_isp
].User
);
384 strcpy(logconn
.phone
,IspData
[current_isp
].Phone
);
386 if (!strlen(logconn
.shortname
))
387 strcpy(logconn
.shortname
,"empty");
388 if (!strlen(logconn
.longname
))
389 strcpy(logconn
.longname
,"empty");
390 if (!strlen(logconn
.user
))
391 strcpy(logconn
.user
,"empty");
392 if (!strlen(logconn
.phone
))
393 strcpy(logconn
.phone
,"empty");
397 if ((!starttime
)&&(isonline
)) {
399 logconn
.end
=time(NULL
);
402 make_delayed_update();
407 /* If we are on-line. Print the time we are */
410 i
= currenttime
- starttime
;
414 if (TimerDivisor
== 1)
415 if (i
> 59 * 60 + 59)
419 hour
= (i
/ 60) % 100;
420 i
= hour
* 100 + minute
;
422 DrawTime (i
, currenttime
% 2);
423 /* We are online, so we can check for send/recv packets */
425 get_statistics (active_interface
, &ppp_recv
, &ppp_send
,
426 &ppp_rbytes
, &ppp_sbytes
);
430 if (ppp_send
!= ppp_sl
)
431 SetOnLED (LED_PPP_TX
);
433 SetOffLED (LED_PPP_TX
);
435 if (ppp_recv
!= ppp_rl
)
436 SetOnLED (LED_PPP_RX
);
438 SetOffLED (LED_PPP_RX
);
443 /* Every five seconds we check to load on the line */
445 if (currenttime
- timetolog
>= 0) {
446 timetolog
=currenttime
+ 60;
450 if ((currenttime
- ppptime
>= 0) || (ppptime
== 0))
453 ppptime
= currenttime
+ updaterate
;
455 ppp_history
[PPP_STATS_HIS
][0] = ppp_rbytes
- ppp_orbytes
;
456 ppp_history
[PPP_STATS_HIS
][1] = ppp_sbytes
- ppp_osbytes
;
458 ppp_orbytes
= ppp_rbytes
;
459 ppp_osbytes
= ppp_sbytes
;
461 DrawStats (54, 17, 5, 32);
463 for (j
= 1; j
< 55; j
++)
465 ppp_history
[j
- 1][0] = ppp_history
[j
][0];
466 ppp_history
[j
- 1][1] = ppp_history
[j
][1];
468 if (currenttime
> speed_ind
)
470 DrawLoadInd ((ppp_history
[54][0] + ppp_history
[54][1]) / updaterate
);
478 while (XPending (display
))
480 XNextEvent (display
, &Event
);
487 XCloseDisplay (display
);
488 while (start_child
| stop_child
)
490 i
= waitpid (0, &status
, WNOHANG
);
493 if (i
== start_child
)
500 i
= CheckMouseRegion (Event
.xbutton
.x
, Event
.xbutton
.y
);
510 ButtonDown (BUT_REW
);
521 i
= CheckMouseRegion (Event
.xbutton
.x
, Event
.xbutton
.y
);
522 // Button but_stat omhoogdoen!
539 if (i
== but_stat
&& but_stat
>= 0)
547 copyXPMArea (ERR_SRC_X
+28, ERR_SRC_Y
+9,
549 ERR_DEST_X
, ERR_DEST_Y
);
551 start_child
= execCommand (IspData
[current_isp
].StartAction
);
552 SetWaitLED (LED_PPP_POWER
);
553 waittime
= ORANGE_LED_TIMEOUT
+ currenttime
;
560 stop_child
= execCommand (IspData
[current_isp
].StopAction
);
568 current_isp
= num_isps
- 1;
578 if (current_isp
== num_isps
)
584 if (Event
.xbutton
.button
==Button1
)
606 DrawTime (int i
, int j
)
611 copyXPMArea (TIMER_SZE_X
* ((i
/ k
) % 10) + 1, TIMER_SRC_Y
,
613 TIMER_DES_X
+ 6 * 0, TIMER_DES_Y
);
615 copyXPMArea (TIMER_SZE_X
* ((i
/ k
) % 10) + 1, TIMER_SRC_Y
,
617 TIMER_DES_X
+ 6 * 1, TIMER_DES_Y
);
622 copyXPMArea (62, TIMER_SRC_Y
,
624 TIMER_DES_X
+ 6 * 2 + 1, TIMER_DES_Y
);
626 copyXPMArea (63, TIMER_SRC_Y
,
628 TIMER_DES_X
+ 6 * 2 + 1, TIMER_DES_Y
);
630 copyXPMArea (TIMER_SZE_X
* ((i
/ k
) % 10) + 1, TIMER_SRC_Y
,
632 TIMER_DES_X
+ 6 * 2 + 4, TIMER_DES_Y
);
634 copyXPMArea (TIMER_SZE_X
* ((i
/ k
) % 10) + 1, TIMER_SRC_Y
,
636 TIMER_DES_X
+ 6 * 3 + 4, TIMER_DES_Y
);
640 DrawStats (int num
, int size
, int x_left
, int y_bottom
)
646 pixels_per_byte
= size
;
647 for (j
= 0; j
< num
; j
++)
648 if ((ppp_history
[j
][0]+ppp_history
[j
][1]) > pixels_per_byte
)
649 pixels_per_byte
= ppp_history
[j
][0] + ppp_history
[j
][1];
651 pixels_per_byte
/= size
;
653 for (k
= 0; k
< num
; k
++)
654 for (j
= 0; j
< size
; j
++)
656 if (j
< (ppp_history
[k
][0] / pixels_per_byte
))
657 copyXPMArea (58 + 2, 92, 1, 1, k
+ x_left
, y_bottom
- j
);
658 else if (j
< (ppp_history
[k
][0] + ppp_history
[k
][1]) / pixels_per_byte
)
659 copyXPMArea (58 + 1, 92, 1, 1, k
+ x_left
, y_bottom
- j
);
661 copyXPMArea (58 + 0, 92, 1, 1, k
+ x_left
, y_bottom
- j
);
666 PrintLittle (int i
, int *k
)
673 copyXPMArea (12 * 5, ERR_SRC_Y
, 4, 8, *k
, ERR_DEST_Y
);
677 copyXPMArea (45, ERR_SRC_Y
, 5, 8, *k
, ERR_DEST_Y
);
681 copyXPMArea (i
* 5 - 5, ERR_SRC_Y
, 5, 8, *k
, ERR_DEST_Y
);
687 DrawSpeedInd (char *speed_action
)
695 fp
= popen (speed_action
, "r");
701 while (fgets (temp
, 128, fp
))
706 if ((p
= strstr (temp
, "CONNECT")))
708 linespeed
= atoi (p
+ 8);
713 i
= (linespeed
% 1000) / 100;
718 copyXPMArea (ERR_SRC_X
+50, ERR_SRC_Y
, 5, 8, k
, ERR_DEST_Y
);
722 PrintLittle (linespeed
% 10, &k
);
732 DrawLoadInd (int speed
)
738 for (i
= 0; i
< 5; i
++)
739 PrintLittle (-1, &k
);
745 PrintLittle (speed
% 10, &k
);
757 "\nyawmppp - Yet Another Window Maker PPP dock applet\n\n");
759 "version %s\n\n",VERSION
);
760 fprintf (stderr
, "usage:\n");
761 fprintf (stderr
, "-h this help screen\n");
762 fprintf (stderr
, "-i <device> (ppp0, ppp1, etc)\n");
763 fprintf (stderr
, "-t set the on-line timer to MM:SS instead of HH:MM\n");
764 fprintf (stderr
, "-u <update rate> (1..10), default 5 seconds\n");
765 fprintf (stderr
, "-v print the version number\n");
766 fprintf (stderr
, "-paranoid be paranoid about open sockets\n");
767 fprintf (stderr
, "\n");
775 fprintf (stderr
, "%s\n", VERSION
);
786 copyXPMArea (LED_ON_X
, LED_ON_Y
, LED_SZE_X
, LED_SZE_Y
, LED_PWR_X
, LED_PWR_Y
);
789 copyXPMArea (ARR_DN_X
, ARR_DN_Y
+ARR_ACTV
, ARR_W
, ARR_H
, LED_RCV_X
, LED_RCV_Y
);
792 copyXPMArea (ARR_UP_X
, ARR_UP_Y
+ARR_ACTV
, ARR_W
, ARR_H
, LED_SND_X
, LED_SND_Y
);
806 copyXPMArea (LED_OFF_X
, LED_OFF_Y
, LED_SZE_X
, LED_SZE_Y
, LED_PWR_X
, LED_PWR_Y
);
809 copyXPMArea (ARR_DN_X
, ARR_DN_Y
, ARR_W
, ARR_H
, LED_RCV_X
, LED_RCV_Y
);
812 copyXPMArea (ARR_UP_X
, ARR_UP_Y
, ARR_W
, ARR_H
, LED_SND_X
, LED_SND_Y
);
827 copyXPMArea (LED_ERR_X
, LED_ERR_Y
, LED_SZE_X
, LED_SZE_Y
, LED_PWR_X
, LED_PWR_Y
);
841 copyXPMArea (LED_WTE_X
, LED_WTE_Y
, LED_SZE_X
, LED_SZE_Y
, LED_PWR_X
, LED_PWR_Y
);
849 ButtonUp (int button
)
855 copyXPMArea (BUT_V_SRC_X
+BUT_UP_INC
, BUT_V_SRC_Y
,
860 copyXPMArea (BUT_X_SRC_X
+BUT_UP_INC
, BUT_X_SRC_Y
,
865 copyXPMArea (BUT_R_SRC_X
+BUT_UP_INC
, BUT_R_SRC_Y
,
870 copyXPMArea (BUT_F_SRC_X
+BUT_UP_INC
, BUT_F_SRC_Y
,
880 ButtonDown (int button
)
886 copyXPMArea (BUT_V_SRC_X
, BUT_V_SRC_Y
,
891 copyXPMArea (BUT_X_SRC_X
, BUT_X_SRC_Y
,
896 copyXPMArea (BUT_R_SRC_X
, BUT_R_SRC_Y
,
901 copyXPMArea (BUT_F_SRC_X
, BUT_F_SRC_Y
,
913 s
= strlen (IspData
[current_isp
].ShortName
);
914 for (i
= 0; i
< 5; i
++)
917 draw_isp_char (i
, IspData
[current_isp
].ShortName
[i
]);
919 draw_isp_char (i
, ' ');
924 draw_isp_char (int pos
, char letter
)
926 int sx
= 0, sy
= 0, ac
= 0;
928 if ((!ac
) && (letter
>= 'A') && (letter
<= 'Z'))
930 sx
= UPPER_ABC_BASE_X
;
931 sy
= UPPER_ABC_BASE_Y
;
932 sy
+= 8 * ((letter
- 'A') / 12);
933 sx
+= 5 * ((letter
- 'A') % 12);
936 if ((!ac
) && (letter
>= 'a') && (letter
<= 'z'))
938 sx
= LOWER_ABC_BASE_X
;
939 sy
= LOWER_ABC_BASE_Y
;
940 sy
+= 8 * ((letter
- 'a') / 12);
941 sx
+= 5 * ((letter
- 'a') % 12);
944 if ((!ac
) && (letter
>= '0') && (letter
<= '9'))
948 sx
+= 5 * (letter
- '0');
957 copyXPMArea (sx
, sy
, 4, 7, ISP_BASE_X
+ 5 * pos
, ISP_BASE_Y
);
961 sigusr_handler(int signum
)
963 if (signum
==SIGUSR1
) {
966 if (current_isp
>=num_isps
)
982 make_delayed_update(void)
985 if (current_isp
>=num_isps
)