Remove trailing whitespace
[dockapps.git] / yawmppp / src / thinppp / thinppp.c
blob5cebddec0f7c8d4468ba462e84573ac59587d7d2
1 /*
3 YAWMPPP - PPP dock app/helper for WindowMaker
4 Copyright (C) 2000:
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
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <time.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <ctype.h>
36 #include <signal.h>
38 #include <sys/wait.h>
39 #include <sys/stat.h>
40 #include <sys/param.h>
41 #include <sys/types.h>
43 #include <gtk/gtk.h>
44 #include "thinppp.h"
45 #include "ycommon.h"
46 #include "misc.h"
47 #include "isprc.h"
49 #include "dataxpm.xpm"
50 #include "backxpm.xpm"
52 GdkPixmap *origback, *myback ,*datasrc;
53 GtkWidget *mainw,*mda;
55 /* ppp related globals */
57 /* global variables */
59 long starttime;
61 char *ProgName;
62 char *active_interface = "ppp0";
63 int TimerDivisor = 60;
64 int updaterate = 5;
65 int current_isp = 0;
66 int num_isps = 0;
68 int got_sched=0;
69 int caution=1;
71 struct YAWMPPP_ISP_INFO IspData[MAX_ISPS];
73 /* log data */
74 struct LogStruct logconn;
76 /* PPP variables */
78 #define PPP_UNIT 0
79 int ppp_h = -1;
80 int ppp_open = 0;
82 #define PPP_STATS_HIS 23
84 int pixels_per_byte;
85 int ppp_history[PPP_STATS_HIS + 1][2];
87 int pressed_button=-1;
88 int myaction=-1;
90 pid_t stop_child = 0;
91 pid_t start_child = 0;
92 int status;
94 int wx,wy,dragx,dragy,xlimit,ylimit,ipx,ipy;
96 int
97 main(int argc,char **argv) {
99 int i;
101 /* Parse Command Line */
103 ProgName = argv[0];
104 if (strlen (ProgName) >= 5)
105 ProgName += (strlen (ProgName) - 5);
107 for (i = 1; i < argc; i++) {
108 char *arg = argv[i];
110 if (*arg == '-') {
111 switch (arg[1]) {
112 case 'c':
113 caution=1;
114 break;
115 case 'd':
116 if (strcmp (arg + 1, "display")) {
117 usage ();
118 exit (1);
120 break;
121 case 'g':
122 if (strcmp (arg + 1, "geometry")) {
123 usage ();
124 exit (1);
126 break;
127 case 'i':
128 if (!argv[i + 1]) {
129 usage ();
130 exit (1);
132 if (strncmp (argv[i + 1], "ppp", 3)) {
133 usage ();
134 exit (1);
136 active_interface = argv[i + 1];
137 i++;
138 break;
139 case 'p':
140 caution=2;
141 break;
142 case 't':
143 TimerDivisor = 1;
144 break;
145 case 'u':
146 i++;
147 if (!argv[i]) {
148 usage ();
149 exit (1);
151 updaterate = atoi (argv[i]);
152 if (updaterate < 1 || updaterate > 10) {
153 usage ();
154 exit (1);
156 break;
157 case 'v':
158 printversion ();
159 exit (0);
160 break;
161 case 'z':
162 printf("Caution level: %d\n",caution);
163 break;
164 default:
165 usage ();
166 exit (0);
167 break;
172 gtk_init(&argc,&argv);
173 gdk_rgb_init();
175 create_thinppp();
177 init_ppp();
178 gtk_idle_add(thinppp,NULL);
179 gtk_main();
180 save_initial_position();
182 gdk_pixmap_unref(origback);
183 gdk_pixmap_unref(datasrc);
184 gdk_pixmap_unref(myback);
186 return 0;
189 void
190 create_thinppp(void)
192 GtkStyle *sty;
193 GdkBitmap *mask;
195 ipx=((gdk_screen_width()-320)/2);
196 ipy=0;
197 read_initial_position();
198 wx=ipx; wy=ipy;
200 mainw=gtk_window_new(GTK_WINDOW_TOPLEVEL);
201 gtk_widget_set_usize(mainw,320,20);
202 gtk_widget_set_uposition(mainw,wx,wy);
203 gtk_widget_realize(mainw);
204 gdk_window_set_decorations(mainw->window,0);
206 xlimit=gdk_screen_width()-320;
207 ylimit=gdk_screen_height()-20;
209 mda=gtk_drawing_area_new();
210 gtk_widget_set_events(mda,GDK_EXPOSURE_MASK|GDK_BUTTON_PRESS_MASK|
211 GDK_BUTTON_RELEASE_MASK|GDK_BUTTON_MOTION_MASK);
212 gtk_drawing_area_size(GTK_DRAWING_AREA(mda),320,20);
213 gtk_container_add(GTK_CONTAINER(mainw),mda);
215 sty=gtk_widget_get_style(mainw);
217 origback=gdk_pixmap_create_from_xpm_d(mainw->window,
218 &mask,
219 NULL,
220 backxpm_xpm);
221 datasrc=gdk_pixmap_create_from_xpm_d(mainw->window,
222 &mask,
223 NULL,
224 dataxpm_xpm);
226 myback=gdk_pixmap_new(mainw->window,320,20,-1);
227 gdk_draw_pixmap(myback,
228 mainw->style->fg_gc[GTK_WIDGET_STATE (mainw)],
229 origback,
230 0, 0,
231 0, 0,
232 320, 20);
234 gtk_signal_connect(GTK_OBJECT(mda),"expose_event",
235 GTK_SIGNAL_FUNC(exposed),NULL);
237 gtk_signal_connect(GTK_OBJECT(mda),"button_press_event",
238 GTK_SIGNAL_FUNC(bpress),NULL);
239 gtk_signal_connect(GTK_OBJECT(mda),"button_release_event",
240 GTK_SIGNAL_FUNC(brelease),NULL);
241 gtk_signal_connect(GTK_OBJECT(mda),"motion_notify_event",
242 GTK_SIGNAL_FUNC(bmotion),NULL);
244 gtk_signal_connect(GTK_OBJECT(mainw),"destroy",
245 GTK_SIGNAL_FUNC(wdestroy),NULL);
247 gtk_widget_show(mda);
248 gtk_widget_show(mainw);
251 gboolean
252 exposed(GtkWidget *w,GdkEventExpose *gee,gpointer data)
254 gdk_draw_pixmap(w->window,
255 w->style->fg_gc[GTK_WIDGET_STATE (w)],
256 myback,
257 gee->area.x, gee->area.y,
258 gee->area.x, gee->area.y,
259 gee->area.width, gee->area.height);
260 return FALSE;
263 void
264 refresh(void)
266 gdk_draw_pixmap(mainw->window,
267 mainw->style->fg_gc[GTK_WIDGET_STATE (mainw)],
268 myback,
269 0, 0,
270 0, 0,
271 320, 20);
272 gdk_flush();
273 gtk_widget_queue_resize(mda);
276 void
277 setled(int index,int type)
279 int sx,sy,dx,dy,w,h;
281 w=LED_SZE_X;
282 h=LED_SZE_Y;
284 switch(index) {
285 case LED_PPP_POWER:
286 dx=LED_PWR_X;
287 dy=LED_PWR_Y;
288 break;
289 case LED_PPP_TX:
290 dx=LED_SND_X;
291 dy=LED_SND_Y;
292 break;
293 case LED_PPP_RX:
294 dx=LED_RCV_X;
295 dy=LED_RCV_Y;
296 break;
297 default:
298 return;
301 switch(type) {
302 case LED_GREEN:
303 sx=LED_ON_X;
304 sy=LED_ON_Y;
305 break;
306 case LED_YELLOW:
307 sx=LED_WTE_X;
308 sy=LED_WTE_Y;
309 break;
310 case LED_RED:
311 sx=LED_ERR_X;
312 sy=LED_ERR_Y;
313 break;
314 case LED_DARK:
315 sx=LED_OFF_X;
316 sy=LED_OFF_Y;
317 break;
318 default:
319 return;
322 paste_xpm(dx,dy,sx,sy,w,h);
323 refresh();
326 void
327 paste_xpm(int dx,int dy,int sx,int sy,int w,int h)
329 gdk_draw_pixmap(myback,
330 mainw->style->fg_gc[GTK_WIDGET_STATE (mainw)],
331 datasrc,
332 sx, sy,
333 dx, dy,
334 w, h);
337 void
338 init_ppp(void)
340 int i;
341 for (i = 0; i < MAX_ISPS; i++)
342 memset(&IspData[i],0,sizeof(struct YAWMPPP_ISP_INFO));
344 make_config_dir();
345 signal(SIGUSR1,sigusr_handler);
346 signal(SIGHUP,sigusr_handler);
347 signal(SIGINT,sigusr_handler);
348 signal(SIGTERM,sigusr_handler);
350 write_pid_file();
351 clean_guards();
352 grab_isp_info(1);
355 gint
356 thinppp(gpointer data)
358 static int like_a_virgin=1;
359 static int but_stat;
360 static long currenttime;
361 static long lasttime;
362 static long waittime;
363 static long ppptime;
364 static int hour, minute;
365 static long timetolog;
366 static long ppp_send, ppp_sl = -1;
367 static long ppp_recv, ppp_rl = -1;
368 static long ppp_sbytes, ppp_rbytes;
369 static long ppp_osbytes, ppp_orbytes;
370 static struct stat st;
371 static int isonline = 0;
372 static int speed_ind = 10;
374 int i,j;
376 if (like_a_virgin) {
377 get_statistics (active_interface, &ppp_rl, &ppp_sl,
378 &ppp_orbytes, &ppp_osbytes);
379 starttime = 0;
380 currenttime = time (0);
381 ppptime = 0;
382 but_stat = -1;
383 waittime = 0;
384 timetolog=0;
386 /* 888k8 on bottom */
387 paste_xpm(ERR_DEST_X,ERR_DEST_Y,ERR_SRC_X+28,ERR_SRC_Y+9,25,8);
388 DrawISPName ();
389 refresh();
391 like_a_virgin=0;
394 lasttime = currenttime;
395 currenttime = time (0);
397 /* Check if any child has left the playground */
398 i = waitpid (0, &status, WNOHANG);
399 if (i == stop_child && stop_child != 0) {
400 starttime = 0;
402 setled(LED_PPP_POWER,LED_DARK);
403 setled(LED_PPP_RX,LED_DARK);
404 setled(LED_PPP_TX,LED_DARK);
405 /* 888k8 on bottom */
406 paste_xpm(ERR_DEST_X,ERR_DEST_Y,ERR_SRC_X+28,ERR_SRC_Y+9,25,8);
407 refresh();
408 stop_child = 0;
410 if (i == start_child && start_child != 0) {
411 if (WIFEXITED (status)) {
412 if (WEXITSTATUS (status) == 10) {
413 starttime = 0;
414 /* 88k8 on bottom */
415 paste_xpm(ERR_DEST_X,ERR_DEST_Y,ERR_SRC_X+28,ERR_SRC_Y+9,25,8);
416 setled(LED_PPP_POWER,LED_DARK);
417 DrawTime (0, 1);
418 refresh();
420 start_child = 0;
424 /* On-line detectie! 1x per second */
426 if (currenttime != lasttime) {
427 i = 0;
429 if (stillonline (active_interface)) {
430 i = 1;
431 if (!starttime) {
432 starttime = currenttime;
434 if (stat (STAMP_FILE, &st) == 0)
435 starttime = st.st_mtime;
437 setled(LED_PPP_POWER,LED_GREEN);
438 waittime = 0;
440 /* 88k8 on bottom */
441 paste_xpm(ERR_DEST_X,ERR_DEST_Y,ERR_SRC_X+28,ERR_SRC_Y+9,25,8);
443 if (IspData[current_isp].SpeedAction)
444 DrawSpeedInd (IspData[current_isp].SpeedAction);
446 speed_ind = currenttime + 10;
447 refresh();
450 if (!i && starttime) {
451 starttime = 0;
452 setled(LED_PPP_POWER,LED_RED);
453 logconn.status=1;
454 /* Error */
455 paste_xpm(ERR_DEST_X,ERR_DEST_Y,ERR_SRC_X,ERR_SRC_Y+9,25,8);
456 if (IspData[current_isp].IfDownAction)
457 execCommand (IspData[current_isp].IfDownAction);
458 refresh();
462 if (waittime && waittime <= currenttime) {
463 setled(LED_PPP_POWER,LED_RED);
464 refresh();
465 waittime = 0;
468 if ((starttime)&&(!isonline)) {
469 isonline=1;
471 logconn.start=time(NULL);
472 logconn.status=0;
473 strcpy(logconn.longname,IspData[current_isp].LongName);
474 strcpy(logconn.shortname,IspData[current_isp].ShortName);
475 strcpy(logconn.user,IspData[current_isp].User);
476 strcpy(logconn.phone,IspData[current_isp].Phone);
478 if (!strlen(logconn.shortname))
479 strcpy(logconn.shortname,"empty");
480 if (!strlen(logconn.longname))
481 strcpy(logconn.longname,"empty");
482 if (!strlen(logconn.user))
483 strcpy(logconn.user,"empty");
484 if (!strlen(logconn.phone))
485 strcpy(logconn.phone,"empty");
487 make_guards();
489 if ((!starttime)&&(isonline)) {
490 isonline=0;
491 logconn.end=time(NULL);
492 write_log();
493 if (got_sched)
494 make_delayed_update();
495 if (caution>0)
496 close_ppp();
499 /* If we are on-line. Print the time we are */
500 if (starttime) {
501 i = currenttime - starttime;
502 i /= TimerDivisor;
504 if (TimerDivisor == 1)
505 if (i > 59 * 60 + 59)
506 i /= 60;
508 minute = i % 60;
509 hour = (i / 60) % 100;
510 i = hour * 100 + minute;
512 DrawTime (i, currenttime % 2);
513 /* We are online, so we can check for send/recv packets */
515 get_statistics (active_interface, &ppp_recv, &ppp_send,
516 &ppp_rbytes, &ppp_sbytes);
517 if (caution>1)
518 close_ppp();
520 if (ppp_send != ppp_sl)
521 setled(LED_PPP_TX,LED_GREEN);
522 else
523 setled(LED_PPP_TX,LED_DARK);
525 if (ppp_recv != ppp_rl)
526 setled(LED_PPP_RX,LED_GREEN);
527 else
528 setled(LED_PPP_RX,LED_DARK);
530 ppp_sl = ppp_send;
531 ppp_rl = ppp_recv;
533 /* Every five seconds we check to load on the line */
535 if (currenttime - timetolog >= 0) {
536 timetolog=currenttime + 60;
537 make_guards();
540 if ((currenttime - ppptime >= 0) || (ppptime == 0)) {
541 ppptime = currenttime + updaterate;
543 ppp_history[PPP_STATS_HIS][0] = ppp_rbytes - ppp_orbytes;
544 ppp_history[PPP_STATS_HIS][1] = ppp_sbytes - ppp_osbytes;
546 ppp_orbytes = ppp_rbytes;
547 ppp_osbytes = ppp_sbytes;
549 DrawStats (23, 9, 170, 13);
551 for (j = 1; j < 24; j++) {
552 ppp_history[j - 1][0] = ppp_history[j][0];
553 ppp_history[j - 1][1] = ppp_history[j][1];
555 if (currenttime > speed_ind) {
556 DrawLoadInd ((ppp_history[23][0] + ppp_history[23][1]) / updaterate);
560 refresh();
563 switch (myaction) {
564 case BUT_V:
565 if (!starttime) {
566 /* 888k8 */
567 paste_xpm(ERR_DEST_X,ERR_DEST_Y,ERR_SRC_X+28,ERR_SRC_Y+9,25,8);
568 DrawTime (0, 1);
569 start_child = execCommand (IspData[current_isp].StartAction);
570 setled(LED_PPP_POWER,LED_YELLOW);
571 waittime = ORANGE_LED_TIMEOUT + currenttime;
572 refresh();
574 break;
575 case BUT_X:
576 if (stop_child == 0)
577 stop_child = execCommand (IspData[current_isp].StopAction);
578 break;
579 case BUT_REW:
580 if (!starttime) {
581 current_isp--;
582 if (current_isp < 0)
583 current_isp = num_isps - 1;
584 if (current_isp < 0)
585 current_isp=0;
586 DrawISPName ();
587 refresh();
589 break;
590 case BUT_FF:
591 if (!starttime) {
592 current_isp++;
593 if (current_isp == num_isps)
594 current_isp = 0;
595 DrawISPName ();
596 refresh();
598 break;
599 case BUT_CONF:
600 run_pref_app();
601 break;
602 case BUT_LOG:
603 run_log_app();
604 break;
605 case BUT_KILL:
606 if (!starttime)
607 gtk_widget_destroy(mainw);
608 break;
610 myaction=-1;
612 usleep (50000L);
613 return TRUE;
616 gboolean
617 wdestroy(GtkWidget *w,GdkEvent *ev,gpointer data)
619 int i;
620 while (start_child | stop_child) {
621 i = waitpid (0, &status, WNOHANG);
622 if (i == stop_child)
623 stop_child = 0;
624 if (i == start_child)
625 start_child = 0;
626 usleep (50000l);
628 gtk_main_quit();
629 return FALSE;
632 gboolean
633 bpress(GtkWidget *w,GdkEventButton *geb,gpointer data)
635 int x,y;
637 if (geb==NULL)
638 return FALSE;
640 x=(int)geb->x;
641 y=(int)geb->y;
643 dragx=(int)geb->x_root;
644 dragy=(int)geb->y_root;
646 pressed_button=-1;
648 if (x<50) {
649 pressed_button=BUT_DRAG;
650 grab_me();
653 if (inbox(x,y,BUT_V_X,BUT_V_Y,12,11)) {
654 pressed_button=BUT_V;
655 paste_xpm(BUT_V_X,BUT_V_Y,BUT_V_SRC_X,BUT_V_SRC_Y,12,11);
656 refresh();
657 grab_me();
658 return FALSE;
661 if (inbox(x,y,BUT_X_X,BUT_X_Y,12,11)) {
662 pressed_button=BUT_X;
663 paste_xpm(BUT_X_X,BUT_X_Y,BUT_X_SRC_X,BUT_X_SRC_Y,12,11);
664 refresh();
665 grab_me();
666 return FALSE;
669 if (inbox(x,y,BUT_R_X,BUT_R_Y,12,11)) {
670 pressed_button=BUT_REW;
671 paste_xpm(BUT_R_X,BUT_R_Y,BUT_R_SRC_X,BUT_R_SRC_Y,12,11);
672 refresh();
673 grab_me();
674 return FALSE;
677 if (inbox(x,y,BUT_F_X,BUT_F_Y,12,11)) {
678 pressed_button=BUT_FF;
679 paste_xpm(BUT_F_X,BUT_F_Y,BUT_F_SRC_X,BUT_F_SRC_Y,12,11);
680 refresh();
681 grab_me();
682 return FALSE;
685 if (inbox(x,y,BUT_C_X,BUT_C_Y,12,11)) {
686 pressed_button=BUT_CONF;
687 paste_xpm(BUT_C_X,BUT_C_Y,BUT_C_SRC_X,BUT_C_SRC_Y,12,11);
688 refresh();
689 grab_me();
690 return FALSE;
693 if (inbox(x,y,BUT_L_X,BUT_L_Y,12,11)) {
694 pressed_button=BUT_LOG;
695 paste_xpm(BUT_L_X,BUT_L_Y,BUT_L_SRC_X,BUT_L_SRC_Y,12,11);
696 refresh();
697 grab_me();
698 return FALSE;
701 if (inbox(x,y,BUT_K_X,BUT_K_Y,17,18)) {
702 pressed_button=BUT_KILL;
703 paste_xpm(BUT_K_X,BUT_K_Y,BUT_K_SRC_X,BUT_K_SRC_Y,17,18);
704 refresh();
705 grab_me();
706 return FALSE;
709 return FALSE;
712 gboolean
713 brelease(GtkWidget *w,GdkEventButton *geb,gpointer data)
715 int x,y,c;
716 if (geb==NULL)
717 return FALSE;
719 if (pressed_button<0)
720 return FALSE;
722 x=(int)geb->x;
723 y=(int)geb->y;
724 c=-1;
726 if (inbox(x,y,BUT_V_X,BUT_V_Y,12,11))
727 c=BUT_V;
728 if (inbox(x,y,BUT_X_X,BUT_X_Y,12,11))
729 c=BUT_X;
730 if (inbox(x,y,BUT_R_X,BUT_R_Y,12,11))
731 c=BUT_REW;
732 if (inbox(x,y,BUT_F_X,BUT_F_Y,12,11))
733 c=BUT_FF;
734 if (inbox(x,y,BUT_C_X,BUT_C_Y,12,11))
735 c=BUT_CONF;
736 if (inbox(x,y,BUT_L_X,BUT_L_Y,12,11))
737 c=BUT_LOG;
738 if (inbox(x,y,BUT_K_X,BUT_K_Y,17,18))
739 c=BUT_KILL;
741 ungrab_me();
742 if (c!=pressed_button) {
743 pressed_button=-1;
744 paste_xpm(BUT_V_X,BUT_V_Y,BUT_V_SRC_X+24,BUT_V_SRC_Y,12,11);
745 paste_xpm(BUT_X_X,BUT_X_Y,BUT_X_SRC_X+24,BUT_X_SRC_Y,12,11);
746 paste_xpm(BUT_R_X,BUT_R_Y,BUT_R_SRC_X+24,BUT_R_SRC_Y,12,11);
747 paste_xpm(BUT_F_X,BUT_F_Y,BUT_F_SRC_X+24,BUT_F_SRC_Y,12,11);
748 paste_xpm(BUT_C_X,BUT_C_Y,BUT_C_SRC_X+24,BUT_C_SRC_Y,12,11);
749 paste_xpm(BUT_L_X,BUT_L_Y,BUT_L_SRC_X+24,BUT_L_SRC_Y,12,11);
750 paste_xpm(BUT_K_X,BUT_K_Y,BUT_K_SRC_X+17,BUT_K_SRC_Y,17,18);
751 refresh();
752 return FALSE;
755 myaction=pressed_button;
757 paste_xpm(BUT_V_X,BUT_V_Y,BUT_V_SRC_X+24,BUT_V_SRC_Y,12,11);
758 paste_xpm(BUT_X_X,BUT_X_Y,BUT_X_SRC_X+24,BUT_X_SRC_Y,12,11);
759 paste_xpm(BUT_R_X,BUT_R_Y,BUT_R_SRC_X+24,BUT_R_SRC_Y,12,11);
760 paste_xpm(BUT_F_X,BUT_F_Y,BUT_F_SRC_X+24,BUT_F_SRC_Y,12,11);
761 paste_xpm(BUT_C_X,BUT_C_Y,BUT_C_SRC_X+24,BUT_C_SRC_Y,12,11);
762 paste_xpm(BUT_L_X,BUT_L_Y,BUT_L_SRC_X+24,BUT_L_SRC_Y,12,11);
763 paste_xpm(BUT_K_X,BUT_K_Y,BUT_K_SRC_X+17,BUT_K_SRC_Y,17,18);
764 refresh();
765 pressed_button=-1;
766 return FALSE;
769 gboolean
770 bmotion(GtkWidget *w,GdkEventMotion *geb,gpointer data)
772 if (pressed_button!=BUT_DRAG)
773 return TRUE;
775 if (geb==NULL)
776 return TRUE;
778 wx=wx+((int)geb->x_root-dragx);
779 wy=wy+((int)geb->y_root-dragy);
781 if (wx<0) wx=0;
782 if (wx>xlimit) wx=xlimit;
783 if (wy<0) wy=0;
784 if (wy>ylimit) wy=ylimit;
786 dragx=(int)geb->x_root;
787 dragy=(int)geb->y_root;
789 gdk_window_move(mainw->window,wx,wy);
790 gdk_flush();
791 return FALSE;
794 void
795 grab_me(void)
797 gdk_pointer_grab(mda->window,FALSE,
798 GDK_BUTTON_RELEASE_MASK,
799 NULL,
800 NULL,
801 time(NULL));
804 void
805 ungrab_me(void)
807 gdk_pointer_ungrab(time(NULL));
810 gboolean
811 inbox(int x,int y,int bx,int by,int bw,int bh)
813 if ((x>=bx)&&(y>=by)&&(x<=(bx+bw))&&(y<=(by+bh)))
814 return TRUE;
815 else
816 return FALSE;
819 void
820 sigusr_handler(int signum)
822 save_initial_position();
823 if (signum==SIGUSR1) {
824 if (!starttime) {
825 grab_isp_info(0);
826 if (current_isp>=num_isps)
827 current_isp=0;
828 DrawISPName();
829 refresh();
830 } else {
831 got_sched=1;
832 warn_pref();
835 else {
836 remove_pid_file();
837 exit(0);
841 /* lower level drawing */
843 void
844 DrawISPName (void)
846 int i, s;
848 s = strlen (IspData[current_isp].ShortName);
849 for (i = 0; i < 5; i++)
851 if (s >= (i + 1))
852 draw_isp_char (i, IspData[current_isp].ShortName[i]);
853 else
854 draw_isp_char (i, ' ');
858 void
859 draw_isp_char (int pos, char letter)
861 int sx = 0, sy = 0, ac = 0;
863 if ((!ac) && (letter >= 'A') && (letter <= 'Z'))
865 sx = UPPER_ABC_BASE_X;
866 sy = UPPER_ABC_BASE_Y;
867 sy += 8 * ((letter - 'A') / 12);
868 sx += 5 * ((letter - 'A') % 12);
869 ac = 1;
871 if ((!ac) && (letter >= 'a') && (letter <= 'z'))
873 sx = LOWER_ABC_BASE_X;
874 sy = LOWER_ABC_BASE_Y;
875 sy += 8 * ((letter - 'a') / 12);
876 sx += 5 * ((letter - 'a') % 12);
877 ac = 1;
879 if ((!ac) && (letter >= '0') && (letter <= '9'))
881 sx = DIGIT_BASE_X;
882 sy = DIGIT_BASE_Y;
883 sx += 5 * (letter - '0');
884 ac = 1;
886 if (!ac)
888 sx = SPACE_BASE_X;
889 sy = SPACE_BASE_Y;
892 paste_xpm(ISP_BASE_X + 5 * pos, ISP_BASE_Y, sx, sy, 4, 7);
895 void
896 DrawTime (int i, int j)
898 int k = 1000;
900 paste_xpm(TIMER_DES_X + 6 * 0, TIMER_DES_Y,
901 TIMER_SZE_X * ((i/k)%10)+1, TIMER_SRC_Y,5,7);
902 k=k/10;
904 paste_xpm(TIMER_DES_X + 6 * 1, TIMER_DES_Y,
905 TIMER_SZE_X * ((i/k)%10)+1, TIMER_SRC_Y,5,7);
906 k=k/10;
908 /* colon */
909 if (j)
910 paste_xpm(TIMER_DES_X + 6 * 2 + 1, TIMER_DES_Y,
911 62, TIMER_SRC_Y,1,7);
912 else
913 paste_xpm(TIMER_DES_X + 6 * 2 + 1, TIMER_DES_Y,
914 63, TIMER_SRC_Y,1,7);
916 paste_xpm(TIMER_DES_X + 6 * 2 + 4, TIMER_DES_Y,
917 TIMER_SZE_X * ((i/k)%10)+1, TIMER_SRC_Y,5,7);
918 k=k/10;
920 paste_xpm(TIMER_DES_X + 6 * 3 + 4, TIMER_DES_Y,
921 TIMER_SZE_X * ((i/k)%10)+1, TIMER_SRC_Y,5,7);
924 void
925 DrawStats (int num, int size, int x_left, int y_bottom)
927 int pixels_per_byte;
928 int j, k;
930 pixels_per_byte = size;
931 for (j = 0; j < num; j++)
932 if ((ppp_history[j][0]+ppp_history[j][1]) > pixels_per_byte)
933 pixels_per_byte = ppp_history[j][0] + ppp_history[j][1];
935 pixels_per_byte /= size;
937 for (k = 0; k < num; k++)
938 for (j = 0; j < size; j++) {
939 if (j < (ppp_history[k][0] / pixels_per_byte))
940 paste_xpm(k+x_left, y_bottom-j,HIST_SRC_X+2,HIST_SRC_Y,1,1);
941 else if (j < (ppp_history[k][0] + ppp_history[k][1]) / pixels_per_byte)
942 paste_xpm(k+x_left,y_bottom-j,HIST_SRC_X+1,HIST_SRC_Y,1,1);
943 else
944 paste_xpm(k+x_left,y_bottom-j,HIST_SRC_X,HIST_SRC_Y,1,1);
948 void
949 PrintLittle (int i, int *k)
951 switch (i) {
952 case -1:
953 *k -= 5;
954 paste_xpm(*k,ERR_DEST_Y,12*5,ERR_SRC_Y,4,8);
955 break;
956 case 0:
957 *k -= 5;
958 paste_xpm(*k,ERR_DEST_Y,45,ERR_SRC_Y,5,8);
959 break;
960 default:
961 *k -= 5;
962 paste_xpm(*k,ERR_DEST_Y,i*5 - 5,ERR_SRC_Y,5,8);
963 break;
967 void
968 DrawSpeedInd (char *speed_action)
970 int linespeed, i, k;
971 FILE *fp;
972 char *p;
973 char temp[128];
975 fp = popen (speed_action, "r");
977 if (fp) {
978 linespeed = 0;
980 while (fgets (temp, 128, fp))
983 pclose (fp);
985 if ((p = strstr (temp, "CONNECT")))
986 linespeed = atoi (p + 8);
988 k = ERR_DEST_X+25;
990 i = (linespeed % 1000) / 100;
991 linespeed /= 1000;
992 PrintLittle (i, &k);
994 k -= 5;
995 paste_xpm(k,ERR_DEST_Y,ERR_SRC_X+50,ERR_SRC_Y,5,8);
997 do {
998 PrintLittle (linespeed % 10, &k);
999 linespeed /= 10;
1000 } while (linespeed);
1004 void
1005 DrawLoadInd (int speed)
1007 int i, k;
1009 k = ERR_DEST_X+25;
1010 for (i = 0; i < 5; i++)
1011 PrintLittle (-1, &k);
1013 k = ERR_DEST_X+25;
1015 do {
1016 PrintLittle (speed % 10, &k);
1017 speed /= 10;
1018 } while (speed);
1022 void
1023 make_delayed_update(void)
1025 grab_isp_info(0);
1026 if (current_isp>=num_isps)
1027 current_isp=0;
1028 DrawISPName();
1029 refresh();
1030 got_sched=0;
1033 void
1034 usage (void) {
1035 fprintf (stderr,
1036 "\nyawmppp.thin\nYet Another Window Maker PPP dock applet,\nfor non-Window Maker window managers\n\n");
1037 fprintf (stderr,
1038 "version %s\n\n",VERSION);
1039 fprintf (stderr, "usage:\n");
1040 fprintf (stderr, "-h this help screen\n");
1041 fprintf (stderr, "-i <device> (ppp0, ppp1, etc)\n");
1042 fprintf (stderr, "-t set the on-line timer to MM:SS instead of HH:MM\n");
1043 fprintf (stderr, "-u <update rate> (1..10), default 5 seconds\n");
1044 fprintf (stderr, "-v print the version number\n");
1045 fprintf (stderr, "-paranoid be paranoid about open sockets\n");
1046 fprintf (stderr, "\n");
1049 void
1050 printversion (void)
1052 fprintf (stderr, "%s\n", VERSION);
1055 void
1056 read_initial_position(void)
1058 FILE *f;
1059 char *p,z[256];
1060 p=getenv("HOME");
1061 sprintf(z,"%s/.yawmppp/thin.position",p);
1062 f=fopen(z,"r");
1063 if (!f)
1064 return;
1065 fgets(z,255,f);
1066 ipx=atoi(strtok(z," \t\n"));
1067 ipy=atoi(strtok(NULL," \t\n"));
1068 fclose(f);
1071 void
1072 save_initial_position(void)
1074 FILE *f;
1075 char *p,z[256];
1076 p=getenv("HOME");
1077 sprintf(z,"%s/.yawmppp/thin.position",p);
1078 f=fopen(z,"w");
1079 if (!f)
1080 return;
1081 ipx=wx;
1082 ipy=wy;
1083 fprintf(f,"%d %d\n",ipx,ipy);
1084 fclose(f);