home *** CD-ROM | disk | FTP | other *** search
/ Spanish Scene 4 / SpanishScene4.iso / Programas / DavidBarbion_Ind / backclock_V232c.lha / BackClock / sources / utils.c < prev    next >
C/C++ Source or Header  |  1999-06-11  |  12KB  |  358 lines

  1. /*****************************************************************************
  2.  * 
  3.  * Nom                          : utils.c
  4.  * desc                         : utilitaires projet
  5.  *
  6.  * version                      : $VER: utils.c 2.3ß5 (05.06.99)
  7.  *
  8.  * ver 2.1:   error handling
  9.  * ver 2.2:   some bug fixed
  10.  * ver 2.2ß2: stay in the background now
  11.  * ver 2.2ß3: LoadConf used a uninitialized variable (pointer to WBScreen)
  12.  * ver 2.3ß4: problem when notifyint was not present
  13.  *            draw or not the seconds arrow
  14.  * ver 2.3ß5: redraw when window (de)selected
  15.  *            uses rtracker.library for allocation
  16.  *
  17.  *****************************************************************************
  18.  */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <exec/exec.h>
  24. #include <exec/ports.h>
  25. #include <exec/io.h>
  26. #include <exec/tasks.h>
  27. #include <exec/libraries.h>
  28. #include <dos/datetime.h>
  29. #include <dos/dosextens.h>
  30. #include <workbench/workbench.h>
  31. #include <libraries/notifyintuition.h>
  32. //#include <libraries/rtracker.library>
  33. #include <clib/rtracker_protos.h>
  34. #include <pragmas/rtracker_pragmas.h>
  35.  
  36. #include <proto/exec.h>
  37. #include <proto/intuition.h>
  38. #include <proto/dos.h>
  39. #include <proto/graphics.h>
  40. #include <proto/gadtools.h>
  41. #include <proto/icon.h>
  42. #include "utils.h"
  43. #include "timer.h"
  44. #include "tracewin.h"
  45. #include "notify.h"
  46. #include "partial.h"
  47. #include "conf.h"
  48. #include "obp.h"
  49.  
  50. struct Library * NotifyIntuitionBase ;
  51. struct Library * RTrackerBase ;
  52.  
  53. char * ptrTime = NULL ;
  54. struct DateTime * dt = NULL ;
  55.  
  56. idWin * init(WBArg * appName) {
  57.   /* initalize the main window
  58.    * and the project
  59.    */
  60.   struct Screen  * WBScreen = NULL ;                 // lock on WB
  61.   idWin * prjWin = NULL ;                            // main prj
  62.   
  63.   if ((RTrackerBase = OpenLibrary("rtracker.library",0)) == NULL) {
  64.     printf("Can't find rtracker.library\n") ;
  65.     return(NULL) ;  
  66.   }
  67.     
  68.   InitResource() ;
  69.   
  70.   if ((prjWin = NewAllocMem(sizeof(idWin), MEMF_PUBLIC|MEMF_CLEAR)) == NULL) { 
  71.     close(prjWin) ;
  72.     return(NULL) ;
  73.   }
  74.     
  75.   if ((prjWin->date = NewAllocMem(sizeof(struct DateStamp), MEMF_PUBLIC)) == NULL) {
  76.     close(prjWin) ;
  77.     return(NULL) ;
  78.   }
  79.     
  80.   if ((prjWin->Notify = NewAllocMem(sizeof(struct IntNotifyRequest), MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {
  81.     close(prjWin) ;
  82.     return(NULL) ;
  83.   }
  84.   
  85.   
  86.   if ((NotifyIntuitionBase = NewOpenLibrary(NOTIFYINTUITIONNAME, NOTIFYINTUITION_VMIN)) != NULL) {
  87.         /* open the window
  88.          */
  89.     
  90.     WBScreen = LockPubScreen("Workbench") ;
  91.     top = WBScreen->BarHeight ; 
  92.     prjWin->wb   = WBScreen ;
  93.     LoadConf(prjWin) ;
  94.     init_bitmap(prjWin) ;
  95.     prjWin->win = OpenWindowTags(NULL, WA_Left,   prjWin->backWin.posX,
  96.                                        WA_Top,         prjWin->backWin.posY,
  97.                                        WA_Width,       prjWin->backWin.width,
  98.                                        WA_Height,      prjWin->backWin.height,
  99.                                        WA_IDCMP,       IDCMP,
  100.                                        WA_MinWidth,      50,
  101.                                        WA_MinHeight,   50,
  102.                                        WA_MaxHeight,   MAXH,
  103.                                        WA_MaxWidth,    MAXH,
  104.                                        WA_Flags,       WFLG,
  105.                                        WA_ScreenTitle, TXT_SCRTITLE,
  106.                                        WA_NewLookMenus, TRUE,
  107.                                        WA_PubScreenName, "Workbench", TAG_DONE) ;
  108.     
  109.     szgdg.Height = top - 1 ;
  110.     szgdg.NextGadget = &tagdg ;
  111.     tagdg.LeftEdge  = prjWin->win->Width /2 ;
  112.     tagdg.TopEdge   = prjWin->win->Height - 20 ;
  113.     tagdg.Width     = prjWin->win->Width /2 ;
  114.     tagdg.Height    = 20 ;
  115.   
  116.     AddGList(prjWin->win, &szgdg, 0, 2, NULL) ;
  117.    
  118.     RefreshGList(&szgdg, prjWin->win, NULL, -1) ;
  119.                                                                                              
  120.     if (prjWin->win == NULL)
  121.       return(NULL) ;
  122.    
  123.     if (prjWin->wb)
  124.       UnlockPubScreen(NULL, WBScreen) ;
  125.     
  126.                                                                                                 
  127.           // initialisation ...  (fenetre...)
  128.   }else {
  129.     ez_req("Backclock Error", "Couldn't open notifyintuition.library V2", "Quit", NULL ) ;
  130.     if (prjWin->Notify) NewFreeVec(prjWin->Notify) ;
  131.     prjWin->Notify = NULL ;
  132.     close(prjWin) ;
  133.     return(NULL) ;
  134.   }
  135.   
  136.   if (!initTimer(prjWin))   { close(prjWin) ; return(NULL) ;}
  137.   if (!startNotify(prjWin)) { close(prjWin) ; return(NULL) ;}
  138.   if ((ptrTime = NewAllocMem(LEN_DATSTRING, MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {close(prjWin) ; return(NULL) ;}
  139.   if ((dt =      NewAllocMem(sizeof(struct DateTime),  MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {close(prjWin) ; return(NULL) ;}
  140.   getDate(prjWin) ;
  141.   initwin(prjWin) ;
  142.   return prjWin ;
  143. }
  144.  
  145. void close(idWin * prj) {
  146.   int i;
  147.   if (prj) {                              // struct prj existe (<>0) 
  148.     endNotify(prj) ;
  149.     if (prj->win) CloseWindow(prj->win) ; // ferme la fenetre
  150.     free_twin(prj) ; 
  151.     if (prj->date) NewFreeVec(prj->date) ;   // libere datestamp
  152.     if (prj->Notify) NewFreeVec(prj->Notify) ; // libere struct IntNotify
  153.     closeTimer(prj) ;
  154.     
  155.     if (NotifyIntuitionBase) NewCloseLibrary(NotifyIntuitionBase) ;
  156.     /* free pens
  157.      */
  158.     prj->wb = LockPubScreen("Workbench") ;
  159.     for(i = 0; i<NUM_COLORS; i++)
  160.       freePen(prj, prj->backWin.cmap[i].reg) ;
  161.     if (prj->wb) UnlockPubScreen(NULL, prj->wb) ;
  162.     if (ptrTime) NewFreeVec(ptrTime) ;
  163.     free_bitmap(prj) ;
  164.     NewFreeVec(prj) ;                        // libere la memoire
  165.     NewFreeVec(dt) ;
  166.   }
  167.   if (RTrackerBase) {
  168.     /* use rtracker.library to free forgotten allocation
  169.      */
  170.     CloseResource() ;
  171.     CloseLibrary(RTrackerBase) ;
  172.   }
  173. }
  174. void getDate(idWin * prj) {
  175.   // remplit la structure (projet) avec la date
  176.    
  177.   DateStamp(prj->date) ;
  178.   if (ptrTime) {  
  179.     CopyMem(prj->date, &(dt->dat_Stamp), sizeof(struct DateStamp)) ;
  180.     dt->dat_Format = FORMAT_DOS ;
  181.     dt->dat_StrTime = ptrTime ;
  182.     DateToStr(dt) ;
  183.     //CopyMem(ptrTime, prj->datestr, LEN_DATSTRING) ;
  184.     DateToByte(ptrTime, prj) ;
  185.   
  186.   }
  187. }
  188.  
  189. void processwin(idWin* prj) {
  190.   /* variables
  191.    */
  192.   ULONG signals = NULL,                                                                          /* masque des signaux a attendre */
  193.         mask,
  194.         class,                                                                                                                   /* masque des signaux recus */
  195.         sigtimer ;
  196.         
  197.   BOOL  stop    = FALSE ;                                                                                                                /* vrai si ctrl c */
  198.   struct IntuiMessage * msg ;                     /* ptr sur message intuition */
  199.   struct MsgPort      * extPort ;
  200.   struct backMsg      * extmsg ;
  201.   
  202.   extPort = NewCreateMsgPort() ;
  203.   extPort->mp_Node.ln_Name = NewAllocMem(10, MEMF_PUBLIC) ;
  204.   CopyMem("backclock", extPort->mp_Node.ln_Name, 10) ;
  205.   AddPort(extPort) ;
  206.   
  207.   sigtimer =  (1<<(prj->treq->tr_node.io_Message.mn_ReplyPort->mp_SigBit)) ; 
  208.   /* signaux d'attentes
  209.    */
  210.   signals = SIGBREAKF_CTRL_C | 
  211.             (1<<(prj->notifyPort->mp_SigBit)) | 
  212.             (1<<(prj->win->UserPort->mp_SigBit)) | sigtimer|
  213.             (1<<(extPort->mp_SigBit)) ;
  214.   
  215.   runtimer(prj,1) ;
  216.   getDate(prj) ;
  217.   effacer(prj) ;
  218.   retracer(prj) ;
  219.   while(!stop) {
  220.     mask = Wait(signals) ;
  221.     if (mask & SIGBREAKF_CTRL_C) {
  222.       /* recu le signal CTRL C
  223.        */
  224.       stop=TRUE;
  225.     }
  226.     if (mask & (1<<(extPort->mp_SigBit))) {
  227.       // recu message prog externe
  228.       extmsg = (struct backMsg *)GetMsg(extPort) ;
  229.       switch(extmsg->Class) {
  230.         case BC_Quit:        // recu demande d'arret
  231.           stop=TRUE ;
  232.           extmsg->error = OK ;
  233.           break;
  234.       
  235.         case BC_GetPrj:      // recu demande d'envoi du projet
  236.           extmsg->ptrPrj = prj ;
  237.           extmsg->error = OK ;
  238.           break;
  239.       
  240.         case BC_RefreshColors:
  241.           setColors(prj) ;
  242.           break;
  243.       
  244.         case BC_Refresh:     // refresh window (redraw)
  245.           reinit_win(prj) ;
  246.           getDate(prj) ;
  247.           effacer(prj) ;
  248.           retracer(prj) ;
  249.           extmsg->error = OK ;
  250.           break;
  251.       
  252.         case BC_SaveConf:
  253.           SaveEnv(prj, TRUE) ;
  254.           if (prj->lastError) extmsg->error = prj->lastError ;
  255.           break;
  256.       
  257.         case BC_UseConf:
  258.           SaveEnv(prj, FALSE) ;
  259.           if (prj->lastError) extmsg->error = prj->lastError ;
  260.           break;
  261.       
  262.         case BC_SetWindow:
  263.           getDate(prj) ;
  264.           setWindow(prj) ;
  265.           
  266.           AbortIO((struct IORequest*)prj->treq) ;
  267.           WaitIO((struct IORequest*)prj->treq) ;
  268.           if(prj->backWin.drawsec) {
  269.             
  270.             runtimer(prj, 1) ;
  271.           }
  272.           extmsg->error = OK ;
  273.           break;
  274.         
  275.         default:
  276.           break;
  277.       }
  278.       if (extmsg) ReplyMsg((struct Message*)extmsg) ;
  279.     }
  280.     if (mask & (1<<(prj->notifyPort->mp_SigBit))) {
  281.       // attention fermeture du WB
  282.       if (partialClose(prj)) partialOpen(prj) ;
  283.     }
  284.     if (mask & 1<<(prj->win->UserPort->mp_SigBit)) {
  285.       /* recu message d'intuition
  286.        */
  287.       while((msg = GT_GetIMsg(prj->win->UserPort)) != NULL) {
  288.         /* retourne le message
  289.          */
  290.         class = msg->Class ;
  291.         ReplyMsg((struct Message*)msg) ;
  292.         if(class == IDCMP_CHANGEWINDOW || class == IDCMP_MOUSEBUTTONS) {
  293.           
  294.           RemoveGList(prj->win, &szgdg, 2) ;
  295.           szgdg.Width = prj->win->Width ;
  296.           tagdg.LeftEdge = prj->win->Width/2 ;
  297.           tagdg.TopEdge  = prj->win->Height -20 ;
  298.           tagdg.Width    = prj->win->Width /2 ;
  299.           prj->backWin.width = prj->win->Width ;
  300.           prj->backWin.height = prj->win->Height ;      
  301.           prj->backWin.posX = prj->win->LeftEdge ;
  302.           prj->backWin.posY = prj->win->TopEdge ;
  303.           setWindow(prj) ;
  304.           //reinit_win(prj) ; 
  305.           AddGList(prj->win, &szgdg, 0, 2, NULL) ;
  306.           RefreshGList(&szgdg, prj->win, NULL, -1) ;
  307.  
  308.           SaveEnv(prj, TRUE) ;
  309.         }
  310.       }          
  311.     }
  312.     if (CheckIO((struct IORequest*)prj->treq)) {
  313.       WaitIO((struct IORequest*)prj->treq) ;
  314.       AbortIO((struct IORequest*)prj->treq) ;
  315.       WaitIO((struct IORequest*)prj->treq) ;
  316.       getDate(prj) ;
  317.       if(prj->backWin.drawsec)
  318.         runtimer(prj, 1) ;
  319.       else
  320.         runtimer(prj, 60-(long)(prj->sec)) ;
  321.       effacer(prj) ;
  322.       retracer(prj) ;
  323.       //WindowToBack(prj->win) ;
  324.     }  
  325.  
  326.   }
  327.   AbortIO((struct IORequest*)prj->treq) ;
  328.   WaitIO((struct IORequest*)prj->treq) ;
  329.   RemPort(extPort) ;
  330.   NewFreeVec(extPort->mp_Node.ln_Name) ;
  331.   NewDeleteMsgPort(extPort) ;
  332.      
  333. }
  334.  
  335. void ez_req(UBYTE * title, UBYTE * body, UBYTE * gadg, APTR arglist ) {
  336.   struct EasyStruct * ez_rq ;
  337.   
  338.   if (ez_rq = NewAllocMem(sizeof(struct EasyStruct), MEMF_PUBLIC)) {
  339.     if (ez_rq->es_Title = NewAllocMem(strlen(title)+1, MEMF_PUBLIC)) {
  340.      strcpy(ez_rq->es_Title, title) ;
  341.      if (ez_rq->es_TextFormat = NewAllocMem(strlen(body)+1, MEMF_PUBLIC)) {
  342.       strcpy(ez_rq->es_TextFormat, body) ;
  343.       if (ez_rq->es_GadgetFormat = NewAllocMem(strlen(gadg)+1, MEMF_PUBLIC)) {
  344.        strcpy(ez_rq->es_GadgetFormat, gadg) ;
  345.        ez_rq->es_StructSize = sizeof(EasyStruct) ;
  346.        ez_rq->es_Flags = NULL ;
  347.        EasyRequestArgs(NULL, ez_rq, NULL, arglist) ;
  348.        NewFreeVec(ez_rq->es_GadgetFormat) ;
  349.       }
  350.       NewFreeVec(ez_rq->es_TextFormat) ;
  351.      }
  352.      NewFreeVec(ez_rq->es_Title) ;
  353.     }
  354.     NewFreeVec(ez_rq) ;
  355.   }
  356. }
  357.  
  358.