home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / commodities / superdark / main / commodities.c next >
C/C++ Source or Header  |  1993-03-20  |  9KB  |  354 lines

  1. /************************************************************************/
  2. /*                                    */
  3. /*    File input.c                            */
  4. /*                                    */
  5. /************************************************************************/
  6. /*                                     */
  7. /*                                    */
  8. /*                                    */
  9. /************************************************************************/
  10.  
  11. #include <exec/types.h>
  12. #include <dos/dos.h>
  13. #include <exec/interrupts.h>
  14. #include <devices/input.h>
  15. #include <devices/inputevent.h>
  16. #include <intuition/intuition.h>
  17. #include <intuition/intuitionbase.h>
  18. #include <graphics/view.h>
  19. #include <libraries/commodities.h>
  20. #include <libraries/dos.h>
  21. #include <libraries/dosextens.h>
  22. #include "/includes/struct.h"
  23. #include "/includes/tom_gadget.h"
  24.  
  25. #include <clib/commodities_protos.h>
  26. #include <dos/rdargs.h>
  27. #include "gid_main.h"
  28.  
  29. /* I don't want to include alib_protos.h, because this will make warning */
  30. /* with all printf()                            */
  31. CxObj *HotKey(UBYTE *,struct MsgPort *,long);
  32.  
  33. #define reg register
  34. #define    NOK    1
  35. #define    OK    0
  36.  
  37. #define    EVT_HOTKEY    1
  38. #define    EVT_BLANKKEY    2
  39.  
  40. #define    ABS(x)    (x<0?x:-x)
  41.  
  42. struct NewBroker newbroker = {
  43.     NB_VERSION,
  44.     "SuperDark",
  45.     "SuperDark V1.4",
  46.     "Multi-modules Screen Blanker",
  47.     NBU_NOTIFY|NBU_UNIQUE,COF_SHOW_HIDE,0,NULL,0};
  48. extern    struct    tom_gadget    my_gadg[];
  49.  
  50. struct my_data    my_info;
  51. struct    MsgPort    *broker_mp=NULL;
  52. CxObj    *broker,*cocustom;
  53. CxObj    *coshow=NULL;
  54. CxObj    *coblank=NULL;
  55. ULONG    signal=0;
  56. ULONG    cosigflag,cxobjsignal;
  57. extern    ULONG    cxsigflag;
  58.  
  59. UBYTE    inputopen = -1;
  60. struct    IOStdReq *inputreq = NULL;
  61. struct Interrupt myhandler;        /* input handler */
  62. long    lsec=0;
  63.  
  64. extern    struct    Library    *IntuitionBase;
  65. extern    struct    Library    *CxBase;
  66.  
  67. static    int    waiting=0;
  68. extern    UBYTE    flg_debug;
  69.  
  70. /**********************************************************/
  71. /* This is the input event handler.             */
  72. /**********************************************************/
  73.  
  74. void    __saveds CxFunction(register struct CxMsg *cxm,CxObj *co)
  75. {
  76.     struct InputEvent *ev;
  77.     UWORD    dispatch;
  78.     dispatch=TRUE;
  79.     ev=(struct InputEvent *)CxMsgData(cxm);
  80.     while (ev) {
  81.  
  82.         /* Si il y a mvt de la souris ou frappe de touche, on remet la    */
  83.         /* tempo a 0. Si de plus, on avait envoyer un signal de blank    */
  84.         /* et bien on renvoi un signal de chgt                */
  85.  
  86.           if ((ev->ie_Class == IECLASS_RAWKEY) || (ev->ie_Class == IECLASS_RAWMOUSE)){
  87.             if(my_info.flg_stop==1){
  88.                 if(my_info.flg_send==1){
  89.                    Signal(my_info.task,1<<my_info.sigstop);
  90.                     dispatch=FALSE;
  91.                 }
  92.             }
  93.             waiting = 0;
  94.         }
  95.  
  96.         /* Evenement timer, sert a compter la non activitee du clavier */
  97.  
  98.         if ((ev->ie_Class == IECLASS_TIMER)){
  99.  
  100.             register struct IntuitionBase *ib =(struct IntuitionBase *) IntuitionBase;
  101.             register short  mousey;
  102.             register struct    Screen *scr = ib->FirstScreen;
  103.  
  104.               if ((ev->ie_TimeStamp.tv_secs != lsec)) {
  105.             lsec = ev->ie_TimeStamp.tv_secs;
  106.  
  107.             /* Si on est en mode aleatoire */
  108.             if(my_info.flg_alea==TRUE){
  109.                 if((my_info.flg_stop==1)&&(waiting<my_info.tempo)){
  110.                     waiting=my_info.tempo;
  111.                 }
  112.                     if(waiting<my_info.tempo_end+1+my_info.tempo)
  113.                     waiting = waiting + 1;
  114.             }else{
  115.                     if(waiting<my_info.tempo+1)
  116.                     waiting = waiting + 1;
  117.             }
  118.             my_info.debug1=waiting;
  119.                   }
  120.  
  121.              /* Si coin inferieur gauche, blanking immediat, mais */
  122.              /* seuleument si la souri n'a pas bouge depuis plus  */
  123.              /* de 3 secondes                      */
  124.              my_info.debug2=ib->MouseY;
  125.              if (waiting==2){
  126.  
  127.             mousey = ib->MouseY;
  128.             if (!(scr->ViewPort.Modes & LACE)){
  129.                 mousey >>=1;
  130.             }
  131.             my_info.debug2=scr->Height-mousey;
  132.             if(    (scr->Height-mousey<4)){
  133.                 if(ib->MouseX<2){
  134.                     if(my_info.flg_stop==0){
  135.                         Signal(my_info.task,1<<my_info.sigstart);
  136.                         waiting=my_info.tempo+1;
  137.                     }
  138.                 }
  139.                 /* Si coin inferieur droit...pas de blank */
  140.                 if(scr->Width -ib->MouseX<2){
  141.                     waiting=0;
  142.                 }
  143.               }
  144.              }
  145.  
  146.         }
  147.         ev = ev->ie_NextEvent;
  148.     }
  149.     if(my_info.flg_send==1){
  150.  
  151.         /********************************************************/
  152.         /* Si il n'y a pas eu d'activitee depuis belle lurette    */
  153.         /* on declenche une tempo de non activitee        */
  154.         /********************************************************/
  155.         
  156.         if((waiting==my_info.tempo)&&(my_info.flg_stop==0)){
  157.             Signal(my_info.task,1<<my_info.sigstart);
  158.             waiting++;
  159.         }
  160.     
  161.         /********************************************************/
  162.         /* Si on arrive a la fin de la deuxieme temporisation,    */
  163.         /* on indique qu'il faut  arreter l'effet en cours    */
  164.         /* mais on remet la tempo juste avant la valeur maximum    */
  165.         /* de non activitee, pour que tout de suite un autre     */
  166.         /* effet soit lance (Mode "aleatoire")            */
  167.         /********************************************************/
  168.         if((waiting==my_info.tempo_end+my_info.tempo)&&(my_info.flg_alea==TRUE)){
  169.                 Signal(my_info.task,1<<my_info.signext);
  170.             waiting = my_info.tempo+1;
  171.         }
  172.     }
  173.     if(dispatch){
  174.         DivertCxMsg(cxm,co,co);
  175.     }
  176. }
  177.  
  178.  
  179. /************************************************************ SetHotKey() */
  180.  
  181. CxObj    *SetHotKey(UBYTE *name,CxObj *co,LONG ev)
  182. {
  183.      if (co){
  184.         RemoveCxObj (co);
  185.         DeleteCxObj (co);
  186.     }
  187.     if((co=HotKey(name,broker_mp,ev))==0){
  188.         printd("Hotkey failed\n");
  189.         return(NULL);
  190.     }
  191.     printd("Hotkey:%s\n",name);
  192.     AttachCxObj(broker,co);
  193.     return co;
  194. }
  195. /************************************************************ reset_key() */
  196. /* This function reset the hot key */
  197. /* input:the pos.   of the element */
  198. /* in the my_gadg array           */
  199.  
  200. void    ResetKey(int    id)
  201. {
  202.     CxObj    *co;
  203.     LONG    ev;
  204.  
  205.     if(id==GID_CXPOPKEY){
  206.         co=coshow;
  207.         ev=EVT_HOTKEY;
  208.     }
  209.     if(id==GID_BLANKKEY){
  210.         co=coblank;
  211.         ev=EVT_BLANKKEY;
  212.     }
  213.     SetHotKey((UBYTE *)my_gadg[id].p_data,co,ev);
  214. }
  215.  
  216. /********************************************************/
  217. /* This is the initialisation of my input event handler */
  218. /********************************************************/
  219.  
  220. int    init_input_ev(int    argc,char **argv)
  221. {
  222.     UBYTE    **ttypes;
  223.  
  224.     my_info.task=(struct Task *)FindTask(0);
  225.     broker_mp=(struct MsgPort *)CreateMsgPort ();
  226.     if(broker_mp==0){
  227.         printf("Create Msg port failed\n");
  228.         return(TRUE);
  229.     }
  230.     cxsigflag=1L<<broker_mp->mp_SigBit;
  231.     newbroker.nb_Port=broker_mp;
  232.     ttypes=ArgArrayInit(argc,argv);
  233.  
  234.     /* Find a tool type with CX_PRIORITY */
  235. /*    newbroker.nb_Pri=0;*/
  236.     newbroker.nb_Pri=(BYTE)ArgInt(ttypes,"CX_PRIORITY",0);
  237.  
  238.     /* If this CxBroker fail, there is bug chance that's because */
  239.     /* another SuperDark is running. So, the other one will open */
  240.     /* his window!                             */
  241.     if((broker=CxBroker(&newbroker,NULL))==0){
  242.         return(TRUE);
  243.     }
  244.     /* Creation of the hotkey Cxobjects...*/
  245.     ResetKey(GID_CXPOPKEY);
  246.     ResetKey(GID_BLANKKEY);
  247.  
  248.     if(cocustom=CxCustom(CxFunction,0)){
  249.         AttachCxObj(broker,cocustom);
  250.         ActivateCxObj(broker,1L);
  251.     }else{
  252.         return(TRUE);
  253.     }
  254.  
  255.     my_info.flg_stop=0;
  256.     my_info.flg_send=1;
  257.  
  258.     my_info.sigstart=AllocSignal(-1);
  259.     if(my_info.sigstart==-1){
  260.         printf("allocsignal failed\n");
  261.         return(TRUE);
  262.     }
  263.     my_info.sigstop=AllocSignal(-1);
  264.     if(my_info.sigstop==-1){
  265.         printf("allocsignal failed\n");
  266.         return(TRUE);
  267.     }
  268.     my_info.signext=AllocSignal(-1);
  269.     if(my_info.signext==-1){
  270.         printf("allocsignal failed\n");
  271.         return(TRUE);
  272.     }
  273.  
  274.     return(FALSE);
  275. }
  276.  
  277. /******************************************************************/
  278. /* This is the liberation of the previously allocated ressources  */
  279. /* of the input event                          */
  280. /******************************************************************/
  281.  
  282. void    free_input_ev()
  283. {
  284.     CxMsg    *msg;
  285.     DeleteCxObjAll(broker);
  286.         if (broker_mp){
  287.         while(msg=(CxMsg *)GetMsg(broker_mp)){
  288.             ReplyMsg((struct Message *)msg);
  289.         }
  290.         DeletePort (broker_mp);
  291.     }
  292.     ArgArrayDone();
  293.     if(my_info.signext!=-1)FreeSignal(my_info.signext);
  294.     if(my_info.sigstart!=-1)FreeSignal(my_info.sigstart);
  295.     if(my_info.sigstop!=-1)FreeSignal(my_info.sigstop);
  296.  
  297. int    ProcessMsg(ULONG    sig)
  298. {
  299.     CxMsg    *msg;
  300.     ULONG    msgid,msgtype;
  301.     int    code_ret;
  302.  
  303.     code_ret=1;
  304.     while(msg=(CxMsg *)GetMsg(broker_mp)){
  305.  
  306.         msgid  =CxMsgID(msg);
  307.         msgtype=CxMsgType(msg);
  308.  
  309.         ReplyMsg((struct Message *)msg);
  310.         printd(" msgtype:%d msgid:%d\n",msgtype,msgid);
  311.         switch(msgtype){
  312.            case CXM_IEVENT:
  313.               switch(msgid){
  314.  
  315.             case EVT_HOTKEY:
  316.                 printd("show()\n");
  317.                 show();
  318.                 break;
  319.             case EVT_BLANKKEY:
  320.                 printd("blank()\n");
  321.                 Signal(my_info.task,1<<my_info.sigstart);
  322.                 waiting=my_info.tempo+1;
  323.                 break;
  324.             default:
  325.                 printd("Unknow id\n");
  326.                 break;
  327.               }
  328.            case CXM_COMMAND:
  329.              switch(msgid){
  330.             case CXCMD_APPEAR:
  331.             case CXCMD_UNIQUE:
  332.                 show();
  333.                 break;
  334.             case CXCMD_DISAPPEAR:
  335.                 press_hide();
  336.                 break;
  337.             case CXCMD_DISABLE:
  338.                 ActivateCxObj(broker,0L);
  339.                 break;
  340.             case CXCMD_ENABLE:
  341.                 ActivateCxObj(broker,1L);
  342.                 break;
  343.             case CXCMD_KILL:
  344.                 stop();
  345.                 code_ret=0;
  346.                 break;
  347.             break;
  348.              }
  349.         }
  350.     }
  351.     return code_ret;
  352. }
  353.