home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / fm2000 / clock.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-04  |  8.8 KB  |  360 lines

  1. /*
  2.      Filemaster - Multitasking directory utility.
  3.      Copyright (C) 2000  Toni Wilen
  4.      
  5.      This program is free software; you can redistribute it and/or
  6.      modify it under the terms of the GNU General Public License
  7.      as published by the Free Software Foundation; either version 2
  8.      of the License, or (at your option) any later version.
  9.      
  10.      This program is distributed in the hope that it will be useful,
  11.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.      GNU General Public License for more details.
  14.      
  15.      You should have received a copy of the GNU General Public License
  16.      along with this program; if not, write to the Free Software
  17.      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #include <proto/all.h>
  21. #include <exec/types.h>
  22. #include <exec/memory.h>
  23. #include <exec/tasks.h>
  24. #include <devices/timer.h>
  25. #include <utility/hooks.h>
  26. #include <strings.h>
  27. #include <stdio.h>
  28. #include "fmnode.h"
  29. #include "config.h"
  30. #include "fmlocale.h"
  31. #include "child.h"
  32. #include "fmdos.h"
  33. #include "commodity.h"
  34.  
  35. #define STARTSLEEP 1
  36. #define SLEEPING 2
  37. #define QUITSLEEP 3
  38. #define QUITSLEEPING 4
  39. #define KILLSLEEP 7
  40.  
  41. void getappdir(struct AppMessage*);
  42. void draw3dbox(struct RastPort*,WORD,WORD,WORD,WORD,WORD);
  43. void textextent(struct RastPort*,UBYTE*,WORD*,WORD*);
  44. WORD getappicon(void);
  45.  
  46. extern struct FMMain fmmain;
  47. extern struct FMConfig *fmconfig;
  48. extern struct FMList fmlist[];
  49. extern struct Locale *locale;
  50. extern UBYTE nformatstring2[];
  51.  
  52. static WORD nostring(void)
  53. {
  54. WORD cnt1;
  55.  
  56. for(cnt1=0;cnt1<LISTS;cnt1++) {
  57.     if(fmlist[cnt1].li&&fmlist[cnt1].li->string.Flags&GACT_ACTIVEGADGET) return(0);
  58. }
  59. return(1);
  60. }
  61.  
  62. void remappicon(void)
  63. {
  64. struct AppMessage *appmsg;
  65.  
  66. if(fmmain.appicon) {
  67.     RemoveAppIcon(fmmain.appicon);
  68.     fmmain.appicon=0;
  69. }
  70. if(fmmain.appiconport) {
  71.     while(appmsg=(struct AppMessage *)GetMsg(fmmain.appiconport)) ReplyMsg((struct Message*)appmsg);
  72.     DeleteMsgPort(fmmain.appiconport);
  73.     fmmain.appiconport=0;
  74. }
  75. }
  76. struct AppIcon *addappicon(void)
  77. {
  78. if(fmmain.appicon&&!(getappicon()&2)) {
  79.     remappicon();
  80.     return(0);
  81. }
  82. if(fmmain.appicon||!(getappicon()&2)) return(fmmain.appicon);
  83. if(!fmmain.dobj) return(0);
  84. remappicon();
  85. fmmain.appiconport=CreateMsgPort();
  86. if(fmmain.appiconport) {
  87.     fmmain.dobj->do_Type=0;
  88.     fmmain.dobj->do_CurrentX=-1;
  89.     fmmain.dobj->do_CurrentY=-1;
  90.     fmmain.appicon=AddAppIconA(0,0,"FM",fmmain.appiconport,0,fmmain.dobj,0);
  91.     if(fmmain.appicon) return(fmmain.appicon);
  92. }
  93. remappicon();
  94. return(0);
  95. }
  96.  
  97. void clockformat(UBYTE *src)
  98. {
  99. LONG afast,achip;
  100. struct DateStamp stamp;
  101. UBYTE sfast[60],schip[60],stotal[60],sdate[60];
  102.  
  103. afast=AvailMem(MEMF_FAST); achip=AvailMem(MEMF_CHIP);
  104. sformat(sfast,nformatstring2,afast);
  105. sformat(schip,nformatstring2,achip);
  106. sformat(stotal,nformatstring2,afast+achip);
  107. DateStamp(&stamp);
  108. longtodatestring(sdate,dstolong(&stamp));
  109. sformatmsg(src,MSG_CLOCK_FORMATSTRING,sfast,schip,stotal,sdate);
  110. }
  111.  
  112. void remsleep(struct Window *swin)
  113. {
  114. if(swin) {
  115.     fmconfig->sleepwindowtop=swin->LeftEdge;
  116.     fmconfig->sleepwindowleft=swin->TopEdge;
  117.     CloseWindow(swin);
  118. }
  119. }
  120.  
  121. WORD setsleep(struct Window **pswin)
  122. {
  123. UBYTE clockstr[400];
  124. WORD apu1,apu2;
  125. extern UBYTE workbench[];
  126. struct Screen *wbscr=0;
  127. struct Window *swin=0;
  128.  
  129. if(!(wbscr=LockPubScreen(0))) return(0);
  130. clockformat(clockstr);
  131. textextent(&wbscr->RastPort,clockstr,&apu1,&apu1);
  132. addappicon();
  133. if((getappicon()&1)||!fmmain.appiconport) {
  134.     apu2=wbscr->Font->ta_YSize+wbscr->WBorTop+1;
  135.     swin=OpenWindowTags(0,
  136.             WA_Left,fmconfig->sleepwindowtop,
  137.             WA_Top,fmconfig->sleepwindowleft,
  138.             WA_MinHeight,apu2,WA_AutoAdjust,TRUE,
  139.             WA_Width,apu1+8*8,WA_Height,apu2,
  140.             WA_IDCMP,IDCMP_MOUSEBUTTONS|IDCMP_CLOSEWINDOW,
  141.             WA_Flags,WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_RMBTRAP|WFLG_NOCAREREFRESH,
  142.             TAG_DONE
  143.     );
  144. }
  145. *pswin=swin;
  146. UnlockPubScreen(0,wbscr);
  147. if(!fmmain.appicon&&!swin) return(0);
  148. return(1);
  149. }
  150.  
  151. struct timerequest *gettimerequest(struct MsgPort **amp)
  152. {
  153. struct timerequest *tr;
  154. struct MsgPort *mp;
  155.  
  156. if (mp=CreateMsgPort()) {
  157.     if (tr=(struct timerequest*)CreateIORequest(mp,sizeof(struct timerequest))) {
  158.         *amp=mp;
  159.         if (!OpenDevice("timer.device",UNIT_VBLANK,(struct IORequest*)tr,0)) return(tr);
  160.         DeleteIORequest(tr);
  161.     }
  162.     DeleteMsgPort(mp);
  163. }
  164. return(0);
  165. }
  166.  
  167. void __saveds fmclock(void)
  168. {
  169. struct ProcMsg *pm;
  170. CxObj *cxmsg;
  171. ULONG msgtype,msgid;
  172. struct Task *task;
  173. struct RastPort rp;
  174. UBYTE clockstr[400];
  175. WORD xx,len;
  176. struct TextExtent txtext;
  177. struct timerequest *tr;
  178. struct MsgPort *tp;
  179. ULONG sigmask,sleepsigmask,sigs,tpsig;
  180. ULONG swinmask,saimask;
  181. WORD timeri;
  182. struct Window *swin=0;
  183. struct IntuiMessage *imsg;
  184. struct AppMessage *appiconmsg=0;
  185. WORD sleepmode;
  186.  
  187. pm=sinitproc();
  188. task=FindTask(0);
  189. SetTaskPri(task,fmconfig->subpriority);
  190. createbrokerall();
  191. if(!(tr=gettimerequest(&tp))) goto error;
  192. tpsig=1L<<tp->mp_SigBit;
  193. sigmask=SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|tpsig;
  194. if(fmmain.cxport) sigmask|=(1L<<fmmain.cxport->mp_SigBit);
  195. fmmain.timeproc=(struct Process*)task;
  196. addappicon();
  197.  
  198. memseti(clockstr,32,399);
  199. InitRastPort(&rp);
  200. SetFont(&rp,fmmain.txtfont);
  201.  
  202. xx=fmconfig->spaceh+fmmain.txtfont->tf_XSize;
  203. timeri=0;sleepmode=0;sleepsigmask=0;
  204. while(fmmain.clock) {
  205.  
  206.     addappicon();
  207.     if(!timeri) {
  208.         tr->tr_node.io_Command=TR_ADDREQUEST;
  209.         tr->tr_time.tv_secs=0;
  210.         tr->tr_time.tv_micro=500000;
  211.         SendIO((struct IORequest*)tr);
  212.         timeri=1;
  213.     }
  214.  
  215.     sigs=Wait(sigmask|sleepsigmask|SIGBREAKF_CTRL_F);
  216.  
  217.     switch(sleepmode)
  218.     {
  219.     case 0:
  220.     if(fmmain.appiconport) {
  221.         appiconmsg=(struct AppMessage*)GetMsg(fmmain.appiconport);
  222.         if(appiconmsg) {
  223.             ScreenToFront(fmmain.naytto);
  224.             if(fmmain.ikkuna) WindowToFront(fmmain.ikkuna);
  225.             getappdir(appiconmsg);
  226.             ReplyMsg((struct Message*)appiconmsg);
  227.             appiconmsg=0;
  228.         }
  229.     }
  230.     if(fmmain.sleepon==5||fmmain.sleepon==6) {
  231.         sleepmode=STARTSLEEP;
  232.         Signal((struct Task*)fmmain.timeproc,SIGBREAKF_CTRL_F);
  233.     }
  234.     break;
  235.     case STARTSLEEP:
  236.     if(!setsleep(&swin)) {
  237.         sleepmode=QUITSLEEP;
  238.     } else {
  239.         sleepmode=SLEEPING;
  240.         swinmask=0;saimask=0;
  241.         if(swin) swinmask=1L<<swin->UserPort->mp_SigBit;
  242.         if(fmmain.appiconport) saimask=1L<<fmmain.appiconport->mp_SigBit;
  243.         sleepsigmask=swinmask|saimask;
  244.     }
  245.     break;
  246.     case SLEEPING:
  247.     if(saimask&&(sigs&saimask)) sleepmode=QUITSLEEP;
  248.     if(swinmask&&(sigs&swinmask)) {
  249.         while((imsg=(struct IntuiMessage*)GetMsg(swin->UserPort))) {
  250.             if(imsg->Class==IDCMP_CLOSEWINDOW) {
  251.                 sleepmode=KILLSLEEP;
  252.             }
  253.             if(imsg->Class==IDCMP_MOUSEBUTTONS&&imsg->Code==MENUDOWN) sleepmode=QUITSLEEP;
  254.             ReplyMsg((struct Message*)imsg);
  255.         }
  256.     }
  257.     break;
  258.     case QUITSLEEP:
  259.     case KILLSLEEP:
  260.     remsleep(swin);
  261.     swin=0;
  262.     if(sleepmode==KILLSLEEP) {
  263.         fmmain.kill=1;
  264.         fmmain.sleepon=0;
  265.     } else {
  266.         fmmain.sleepon=7;
  267.     }
  268.     sleepmode=0;
  269.     Signal((struct Task*)fmmain.myproc,SIGBREAKF_CTRL_F);
  270.     break;
  271.     }
  272.  
  273.     if(sigs&SIGBREAKF_CTRL_C) {
  274.         fmmain.kill=1;
  275.         if(!sleepmode) Signal((struct Task*)fmmain.myproc,SIGBREAKF_CTRL_F);
  276.     }
  277.  
  278.     if(sigs&SIGBREAKF_CTRL_D&&fmmain.broker) {
  279.         DeleteCxObjAll(fmmain.broker);
  280.         fmmain.broker=0;
  281.         createbroker();
  282.     }
  283.  
  284.     if(fmmain.cxport) {
  285.         while(cxmsg=(CxMsg*)GetMsg(fmmain.cxport)) {
  286.             msgtype=CxMsgID(cxmsg);
  287.             msgid=CxMsgType(cxmsg);
  288.             ReplyMsg((struct Message*)cxmsg);
  289.             switch(msgid)
  290.             {
  291.             case CXM_IEVENT:
  292.             if(!sleepmode&&fmmain.fmactive==3&&!fmmain.passhotkey&&nostring()) {
  293.                 fmmain.passhotkey=msgtype;
  294.                 Signal((struct Task*)fmmain.myproc,SIGBREAKF_CTRL_F);
  295.             }
  296.             break;
  297.             case CXM_COMMAND:
  298.                 switch(msgtype)
  299.                 {
  300.                 case CXCMD_APPEAR:
  301.                 if(!sleepmode) {
  302.                     sleepmode=STARTSLEEP;
  303.                 } else {
  304.                     ScreenToFront(fmmain.naytto);
  305.                     WindowToFront(fmmain.ikkuna);
  306.                 }
  307.                 break;
  308.                 case CXCMD_DISAPPEAR:
  309.                 if(!sleepmode) Signal((struct Task*)fmmain.myproc,SIGBREAKF_CTRL_F);
  310.                 break;
  311.                 case CXCMD_KILL:
  312.                 fmmain.kill=1;
  313.                 if(!sleepmode) Signal((struct Task*)fmmain.myproc,SIGBREAKF_CTRL_F);
  314.                 break;
  315.                 case CXCMD_DISABLE:
  316.                 ActivateCxObj(fmmain.broker,FALSE);
  317.                 break;
  318.                 case CXCMD_ENABLE:
  319.                 ActivateCxObj(fmmain.broker,TRUE);
  320.                 break;
  321.                 }
  322.             break;
  323.             }
  324.         }
  325.     }
  326.  
  327.     if(CheckIO((struct IORequest*)tr)) {
  328.         WaitIO((struct IORequest*)tr);
  329.         SetSignal(0,tpsig);
  330.         fmmain.timer++;
  331.         timeri=0;
  332.         if(fmmain.sourcedir->flags&LUPDATEMSG) fmmessage(fmmain.sourcedir);
  333.         clockformat(clockstr);
  334.         if(swin) {
  335.             SetWindowTitles(swin,clockstr,(UBYTE*)-1);
  336.         }
  337.         if(fmmain.ikkuna&&AttemptSemaphore(&fmmain.gfxsema)) {
  338.             len=TextFit(fmmain.rp,clockstr,strlen(clockstr),&txtext,0,1,fmmain.bottomlinewidth-2,32767);
  339.             SetAPen(fmmain.rp,fmconfig->txtpen);
  340.             Move(fmmain.rp,xx,fmmain.bottomliney+fmmain.txtbaseline);
  341.             Text(fmmain.rp,clockstr,len);
  342.             SetAPen(fmmain.rp,fmconfig->backpen);
  343.             if(txtext.te_Width<fmmain.bottomlinewidth) RectFill(fmmain.rp,txtext.te_Width+xx,fmmain.bottomliney,fmmain.bottomlinewidth+xx-3,fmmain.bottomliney+fmmain.txtysize-1);
  344.             ReleaseSemaphore(&fmmain.gfxsema);
  345.         }
  346.     }
  347. }
  348. if(timeri) {
  349.     AbortIO((struct IORequest*)tr);
  350.     WaitIO((struct IORequest*)tr);
  351. }
  352. error:
  353. remsleep(swin);
  354. removebroker();
  355. remappicon();
  356. closedevice((struct IORequest*)tr);
  357. deinitproc(pm);
  358. fmmain.clock=1;
  359. }
  360.