home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ARexxTools / AutoSave.lha / AutoSave / autosave.c next >
Encoding:
C/C++ Source or Header  |  1992-12-05  |  4.4 KB  |  176 lines

  1.  
  2. /* This program is PD. Do with it as you will... */
  3.  
  4.  
  5. struct TextAttr topaz8 = {"topaz.font",8,0x00,0x01};
  6.  
  7. UBYTE *labels[] = {
  8.   (UBYTE *)"Disabled",
  9.   (UBYTE *)"1 minute",
  10.   (UBYTE *)"5 minutes",
  11.   (UBYTE *)"15 minutes",
  12.   (UBYTE *)"1 hour",
  13.   NULL
  14. };
  15.  
  16.  
  17. APTR IntuitionBase,GadToolsBase,IconBase;
  18.  
  19. void run_script(char *script)
  20. {
  21.   char cmd[80];
  22.   ULONG out;
  23.  
  24. /*  out = Open("CON:0/0/200/50/Output",MODE_NEWFILE);
  25.   if (!out) return; */
  26.   strcpy(cmd,"run <NIL: >NIL: sys:rexxc/rx "); strcat(cmd,script);
  27. /*  Execute(cmd,0,out); */
  28.   system(cmd);
  29.   Delay(20); 
  30. /* Close(out); */
  31. }
  32.  
  33. struct NewGadget ng;
  34. struct timerequest timereq; 
  35. struct Window *win;
  36. struct VisualInfo *vi;
  37. struct Gadget *glist,*gad;
  38. struct DiskObject *dob;
  39. struct MsgPort *timeport;
  40.  
  41. void notify(char *str)
  42. {
  43.   struct EasyStruct es;
  44.  
  45.   es.es_StructSize = sizeof(struct EasyStruct);
  46.   es.es_Flags = 0;
  47.   es.es_Title = "Aladdin Autosave";
  48.   es.es_TextFormat = str;
  49.   es.es_GadgetFormat = "OK";
  50.   EasyRequest(win,&es,NULL,NULL);
  51. }
  52.  
  53. void die(char *str)
  54. {
  55.   if (str) notify(str);
  56.   if (dob) FreeDiskObject(dob);
  57.   if (win) CloseWindow(win);
  58.   if (glist) FreeGadgets(glist);
  59.   if (vi) FreeVisualInfo(vi);
  60.   if (timereq.tr_node.io_Device) {
  61.     if (timereq.tr_node.io_Command)
  62.       if (!CheckIO(&timereq)) { 
  63.         AbortIO(&timereq); WaitIO(&timereq); 
  64.       }
  65.     CloseDevice(&timereq);
  66.   }
  67.   if (timeport) DeletePort(timeport);
  68.   if (GadToolsBase) CloseLibrary(GadToolsBase);
  69.   if (IntuitionBase) CloseLibrary(IntuitionBase);
  70.   exit(str?10:0);
  71. }
  72.  
  73. void start_timer(int mins)
  74. {
  75.   timereq.tr_time.tv_secs = mins?10*mins:-1;
  76.   SendIO(&timereq);
  77. }
  78.  
  79. void main(int argc, char *argv[])
  80. {
  81.   extern struct WBStartup *WBenchMsg;
  82.  
  83.   struct IntuiMessage *msg; ULONG class; UWORD code;
  84.   int mins=15,active;
  85.   char *timestr,*script,*title;
  86.  
  87.   if (argc>0) exit(0);        /* abort if CLI */
  88.  
  89.   IconBase = OpenLibrary("icon.library",0);
  90.   if (dob=GetDiskObject(WBenchMsg->sm_ArgList->wa_Name)) {
  91.     if (timestr=FindToolType(dob->do_ToolTypes,"MINUTES")) {
  92.       mins = atoi(timestr);
  93.       if (mins!=0 && mins!=1 && mins!=5 && mins!=15 && mins!=60) mins=15;
  94.     }
  95.     if (!(script=FindToolType(dob->do_ToolTypes,"SCRIPT"))) {
  96.       script = "rexx:AutoSave.rexx";
  97.     }
  98.     if (!(title=FindToolType(dob->do_ToolTypes,"TITLE"))) {
  99.       title = "AutoSave";
  100.     }
  101.   }
  102.  
  103.   IntuitionBase = OpenLibrary("intuition.library",0);
  104.   GadToolsBase = OpenLibrary("gadtools.library",0);
  105.  
  106. /* open window */
  107.  
  108.   vi = GetVisualInfo(LockPubScreen(NULL),TAG_DONE);
  109.  
  110.   gad = CreateContext(&glist);
  111.   ng.ng_VisualInfo = vi; ng.ng_TextAttr = &topaz8;
  112.   ng.ng_LeftEdge = 10; ng.ng_TopEdge = 15;
  113.   ng.ng_Flags = PLACETEXT_RIGHT;
  114.   ng.ng_TextAttr = &topaz8;
  115.  
  116.   switch (mins) {
  117.     case 0: active=0; break; 
  118.     case 1: active=1; break;
  119.     case 5: active=2; break; 
  120.     case 15: active=3; break;
  121.     case 60: active=4; break;
  122.   }
  123.  
  124.   gad = CreateGadget(MX_KIND,gad,&ng,
  125.     GTMX_Labels,labels, GTMX_Spacing,2L, GTMX_Active,active, TAG_DONE);
  126.   if (!gad) die("Couldn't create gadgets");
  127.  
  128.   win = OpenWindowTags(NULL,
  129.     WA_Left,200L, WA_Top,50L, WA_Width,200L, WA_Height,70L,
  130.     WA_IDCMP,MXIDCMP|CLOSEWINDOW,
  131.     WA_Title,title,
  132.     WA_Gadgets, glist,
  133.     WA_ScreenTitle,"Autosave by Michael Warner 1992",
  134.     WA_Flags,WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH|SMART_REFRESH|NOCAREREFRESH,
  135.     WA_PubScreen,NULL, TAG_DONE
  136.     );
  137.   if (!win) die("Couldn't open window");
  138.   GT_RefreshWindow(win,NULL);
  139.  
  140. /* start timer */
  141.  
  142.   timeport = CreatePort(0,0);
  143.   if (OpenDevice("timer.device",0,&timereq,0)) die("Couldn't open timer device");
  144.   timereq.tr_node.io_Message.mn_ReplyPort = timeport;
  145.   timereq.tr_node.io_Command=TR_ADDREQUEST;  
  146.   start_timer(mins);
  147.  
  148.   FOREVER {
  149.     Wait(1L<<win->UserPort->mp_SigBit|1L<<timeport->mp_SigBit);
  150.     if (CheckIO(&timereq)) {    /* timer expired? */
  151.       WaitIO(&timereq); run_script(script);
  152.       start_timer(mins);
  153.     }
  154.     if (msg=GT_GetIMsg(win->UserPort)) {
  155.       class = msg->Class; code = msg->Code; GT_ReplyIMsg(msg);
  156.       switch (class) {
  157.         case CLOSEWINDOW: die(NULL); break;
  158.         case GADGETDOWN:
  159.           switch (code) {
  160.             case 0: mins=0; break;
  161.             case 1: mins=1; break;
  162.             case 2: mins=5; break;
  163.             case 3: mins=15; break;
  164.             case 4: mins=60; break;
  165.           }
  166.           if (!CheckIO(&timereq)) {     /* timer still pending? */
  167.             AbortIO(&timereq); WaitIO(&timereq);
  168.           }
  169.           start_timer(mins);
  170.           break;
  171.       }
  172.     }
  173.   }
  174. }
  175.   
  176.