/* A DefCon epplet that sets the current defense condition based upon your current load. */ #include #define NAME "DefCon" #define VER "0.5" #define INFO "your current defense condition" #define WTH 6 #define HTH 3 #define END 8 #define LDSZ 5 /* we look for the the sound player in the makefile * * if it's not found we use this as the default. */ #if !defined(PLYSND) #define PLYSND "/usr/bin/esdplay" #endif /* where are the number icons? */ #define NUMS "/epplet_data/DefCon/nums" /* what sounds shall we play to be annoying at the varying DefCon levels? */ #define DFC_SNDS EROOT "/epplet_data/DefCon/sounds" #define DFC_1SND PLYSND " " DFC_SNDS "/DefCon1.wav" /* * these sounds are not yet implemented, anyone care to help? * * #define DFC_2SND PLYSND DFC_SNDS "/DefCon2.wav" * #define DFC_3SND PLYSND DFC_SNDS "/DefCon3.wav" * #define DFC_4SND PLYSND DFC_SNDS "/DefCon4.wav" * #define DFC_5SND PLYSND DFC_SNDS "/DefCon5.wav" */ // loads are weird...so define your own at which the DefCon levels are // triggered, or go with the defaults...(they are the ceiling) // i.e. DefCon 5 is between .0-.1, DefCon 4 is between .11 - .2 #define DFC5 0.3 #define DFC4 0.6 #define DFC3 0.9 #define DFC2 1.2 // where are the images??? // first the grays... #define IMG1 EROOT NUMS "/1.png" #define IMG2 EROOT NUMS "/2.png" #define IMG3 EROOT NUMS "/3.png" #define IMG4 EROOT NUMS "/4.png" #define IMG5 EROOT NUMS "/5.png" //then the greens #define IMG1G EROOT NUMS "/1br.png" #define IMG2G EROOT NUMS "/2br.png" #define IMG3G EROOT NUMS "/3br.png" #define IMG4G EROOT NUMS "/4br.png" #define IMG5G EROOT NUMS "/5br.png" //then the colors... #define IMG1C EROOT NUMS "/1red.png" #define IMG2C EROOT NUMS "/2yr.png" #define IMG3C EROOT NUMS "/3yl.png" #define IMG4C EROOT NUMS "/4yg.png" #define COLOR 1 #define GREEN 0 #define STARTDEFCON 5 /* the help message */ #define HELPMSG "usage: DefCon.epplet <-s -c -n -v -h> -s play the sounds using esd when appropriate -c use the colored numbers -n don't use the colored numbers -v show version -h show this help message\n" #define VER_MSG "\n" NAME " Build-" VER "\nSend changes to: Bryan Olmstead [olmsteab@acm.org]\n\n" Epplet_gadget cbutton, mbutton, dctxt, jhelp; struct defcon_data { int focus; //tells us if we have the focus or not int state; //this keeps track, for now, of cmdline stuff int color; //what color scheme are we using? int active; //what is the active defcon? int activeold; //what was the old defcon? Epplet_gadget numary[10]; //an array of number pictures to hide&show when appropriate Epplet_gadget cfg; //the config popup }; static void close_epplet(void *data); static void in_focus(struct defcon_data *dd, Window w); static void out_focus(struct defcon_data *dd, Window w); static void get_defcon(struct defcon_data *dd); void set_color(struct defcon_data *dd, int which); static void config_epplet(struct defcon_data *dd); static void change_color(struct defcon_data *dd); static void change_green(struct defcon_data *dd); static void sound_on(struct defcon_data *dd); static void sound_off(struct defcon_data *dd); static void make_help(void *data) { Epplet_show_about("DefCon"); data = NULL; } int main(int argc, char **argv) { int tmp = 0; struct defcon_data dd; Epplet_Init(NAME, VER, INFO, WTH, HTH, argc, argv, 0); /* make the various things and show when appropriate... */ Epplet_gadget_show(Epplet_create_label(18,2,NAME, 1)); Epplet_gadget_show(Epplet_create_label(52,2,VER, 1)); cbutton = Epplet_create_button(NULL, NULL, 2, 2, 0, 0, "CLOSE", 0, NULL, close_epplet, NULL); mbutton = Epplet_create_button(NULL, NULL, 82, 2, 0, 0, "CONFIGURE", 0, NULL, config_epplet, &dd); jhelp = Epplet_create_button(NULL, NULL, 70, 2, 0, 0, "HELP", 0, NULL, make_help, NULL); /* create the config popup */ dd.cfg = Epplet_create_popup(); Epplet_add_popup_entry(dd.cfg, "Color", NULL, change_color, &dd); Epplet_add_popup_entry(dd.cfg, "No Color", NULL, change_green, &dd); Epplet_add_popup_entry(dd.cfg, "Sound On", NULL, sound_on, &dd); Epplet_add_popup_entry(dd.cfg, "Sound Off", NULL, sound_off, &dd); /* register the various events */ Epplet_timer(get_defcon, &dd, 0.5, "TIMER"); Epplet_register_focus_in_handler(in_focus, &dd); Epplet_register_focus_out_handler(out_focus, &dd); /* initialize some vars */ dd.focus = 0; dd.state = 0; dd.color = 0; dd.active = STARTDEFCON; dd.activeold = STARTDEFCON; /* create the number array */ dd.numary[0] = Epplet_create_image(0,15,20,30,IMG1); dd.numary[1] = Epplet_create_image(18,15,20,30,IMG2); dd.numary[2] = Epplet_create_image(36,15,20,30,IMG3); dd.numary[3] = Epplet_create_image(54,15,20,30,IMG4); dd.numary[4] = Epplet_create_image(72,15,20,30,IMG5); /* parse the command line */ if ( argc > 1 ) { for (tmp=1; tmpfocus = 0; } static void in_focus(struct defcon_data *dd, Window w) { //a dirty fix for the focus problem Epplet_remove_timer("TIMER"); dd->focus = 1; Epplet_gadget_show(cbutton); Epplet_gadget_show(mbutton); Epplet_gadget_show(jhelp); } static void close_epplet(void *data) { Epplet_cleanup(); exit(0); } static void get_defcon(struct defcon_data *dd) { int tmp; FILE *ldavg; char *ldptr; char load[LDSZ]; char *ldpath = "/proc/loadavg"; double fload = 0; ldavg = fopen(ldpath, "r"); ldptr = &load; ldptr = memset(ldptr, '\0', LDSZ); ldptr = fgets(ldptr, LDSZ, ldavg); fclose(ldavg); fload = atof(ldptr); dd->activeold = dd->active; /* light up the current defcon level */ if ( fload <= DFC5 ) dd->active = 5; else if ( fload > DFC5 && fload <= DFC4 ) dd->active = 4; else if ( fload > DFC4 && fload <= DFC3 ) dd->active = 3; else if ( fload > DFC3 && fload <= DFC2 ) dd->active = 2; else if ( fload >= DFC2 ) dd->active = 1; if (dd->activeold != dd->active ) { /* un-light the old defcon level */ Epplet_gadget_hide(dd->numary[dd->activeold+4]); Epplet_gadget_show(dd->numary[dd->activeold-1]); /* light up the new defcon level */ Epplet_gadget_hide(dd->numary[dd->active-1]); Epplet_gadget_show(dd->numary[dd->active+4]); /* do the state stuff here */ if ( dd->state == 1 && dd->active == 1 ) system(DFC_1SND); } Esync(); Epplet_timer(get_defcon, dd, 0.5, "TIMER"); } void set_color(struct defcon_data *dd, int which) { if (which == 1) { //set up the colors... dd->numary[5] = Epplet_create_image(0,15,20,30,IMG1C); dd->numary[6] = Epplet_create_image(18,15,20,30,IMG2C); dd->numary[7] = Epplet_create_image(36,15,20,30,IMG3C); dd->numary[8] = Epplet_create_image(54,15,20,30,IMG4C); dd->numary[9] = Epplet_create_image(72,15,20,30,IMG5G); dd->color = 1; } else { //go with the default green dd->numary[5] = Epplet_create_image(0,15,20,30,IMG1G); dd->numary[6] = Epplet_create_image(18,15,20,30,IMG2G); dd->numary[7] = Epplet_create_image(36,15,20,30,IMG3G); dd->numary[8] = Epplet_create_image(54,15,20,30,IMG4G); dd->numary[9] = Epplet_create_image(72,15,20,30,IMG5G); dd->color = 1; } } static void config_epplet(struct defcon_data *dd) { Epplet_pop_popup(dd->cfg, 0); } static void change_color(struct defcon_data *dd) { //Epplet_gadget_hide(dd->numary[dd->active+4]); set_color(dd, COLOR); Epplet_gadget_show(dd->numary[dd->active+4]); Esync(); } static void change_green(struct defcon_data *dd) { //Epplet_gadget_hide(dd->numary[dd->active+4]); set_color(dd, GREEN); Epplet_gadget_show(dd->numary[dd->active+4]); Esync(); } static void sound_on(struct defcon_data *dd) { dd->state = 1; Esync(); system(DFC_1SND); } static void sound_off(struct defcon_data *dd) { dd->state = 0; Esync(); }