home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 266.lha / SetKey_v2.0 / src / setkey.c < prev    next >
C/C++ Source or Header  |  1989-07-10  |  17KB  |  510 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3. #include <exec/io.h>
  4. #include <exec/memory.h>
  5.  
  6. #include <graphics/gfxbase.h>
  7. #include <graphics/text.h>
  8. #include <graphics/gfxmacros.h>
  9. #include <devices/console.h>
  10. #include <devices/keymap.h>
  11. #include <libraries/dos.h>
  12. #include <libraries/dosextens.h>
  13. #include <intuition/intuition.h>
  14. #include <functions.h>
  15.  
  16. #define IMSG struct IntuiMessage *
  17. #define KEYBASE 63
  18.  
  19. UBYTE the_name[96];
  20. UBYTE buffer[96];
  21. struct GfxBase *GfxBase;
  22. struct IntuitionBase *IntuitionBase;
  23.  
  24. /*  All the intuition structs & screen array data is in intui.c  */
  25.  
  26. #include "externs.h"
  27. #include "i.c"
  28.  
  29.  
  30. main(argc,argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.    struct IntuiMessage *NewMessage; /* msg structure for GetMsg() */
  35.    struct Gadget *the_gadget;
  36.    ULONG class;
  37.    USHORT mode,menunumber,code,pos,quit_sig,val;
  38.    USHORT itemnum,subnum;
  39.    long position;
  40.    struct MenuItem *item;
  41.    char *go;
  42.    SHORT KeepGoing = TRUE;    /* main loop control value */
  43.  
  44.    BOOL modified = FALSE;
  45.    BOOL success;
  46.    short i,j,k,l,q,which;
  47.    long qual_flag,num1,oflg;
  48.    USHORT rawkey;
  49.    short totlen,toolong;
  50.  
  51.    mode = 1;
  52.  
  53.    if(!(IntuitionBase=(struct IntuitionBase *)
  54.                             OpenLibrary("intuition.library",0L)))
  55.       exit(100);
  56.    if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0L)))
  57.       exit(101);
  58.    if((w=OpenWindow(&nw))==NULL)
  59.       exit(102);
  60.    if (OpenConsole(w)!=NULL)     exit(103);
  61.  
  62.    AllocKeyMap();
  63.    if(abort) goto error;
  64.    
  65.    DefaultKeyMap();
  66.    if(abort) goto error;
  67.  
  68.    filestring[0] = NULL;
  69.  
  70.    if(argc == 2) {
  71.        strcpy(filestring, argv[1]);
  72.        strcpy(the_name, dirstring);
  73.        if((!(the_name[strlen(the_name)-1] == '/'))
  74.               && (!(the_name[strlen(the_name)-1] == ':')))
  75.                    strcat(the_name,"/");
  76.        strcat(the_name, filestring);
  77.        if(!LoadKeyMap(the_name)) {
  78.           oflg = AutoRequest(w,&err_txt,NULL,
  79.                      &no_txt,NULL,NULL,330L,60L);
  80.        }
  81.        if(abort) goto error;
  82.     }
  83.  
  84. /*  Initialize the keyboard gadgets.          */
  85.  
  86. for(i=0;i<90;i++) {
  87.    if(i!=89)
  88.       gadget[i].NextGadget = &gadget[i+1];
  89.    gadget[i].LeftEdge = l_edge[i];
  90.    gadget[i].TopEdge = KEYBASE + t_edge[i];
  91.    gadget[i].Width = width[i];
  92.    gadget[i].Height = 11;
  93.    gadget[i].Flags = GADGHNONE;
  94.    gadget[i].Activation = GADGIMMEDIATE;
  95.    gadget[i].GadgetType = BOOLGADGET;
  96.    gadget[i].GadgetRender = NULL;
  97.    gadget[i].SelectRender = NULL;
  98.    gadget[i].GadgetText = NULL;
  99.    gadget[i].MutualExclude = 0;
  100.    gadget[i].SpecialInfo = NULL;
  101.    gadget[i].GadgetID = i;
  102.    gadget[i].UserData = NULL;
  103.    };
  104. gadget[89].NextGadget = NULL;
  105.  
  106. /*----------------------------------------------------------------------*/
  107. /*  Special non-universal gadget initializations.     */
  108.  
  109. /* Return Key */
  110. gadget[89].Height = 13;
  111.  
  112. /* The qualifier keys */
  113. gadget[47].UserData = gadget[48].UserData = gadget[65].UserData = 
  114.   gadget[76].UserData = gadget[81].UserData = gadget[85].UserData = (APTR)1L;
  115. gadget[47].Flags = gadget[48].Flags = gadget[65].Flags = 
  116.   gadget[76].Flags = gadget[81].Flags = gadget[85].Flags = GADGHNONE;
  117.  
  118. /* Disable Amiga Keys!!!!!  */
  119. gadget[84].Flags = gadget[82].Flags = GADGDISABLED;
  120.  
  121. /*----------------------------------------------------------------------*/
  122.  
  123. qual_flag = 0;
  124.  
  125. instring[0] = undostring[0] = NULL;
  126. hexinstring[0] = hexundostring[0] = NULL;
  127.  
  128. /*   Turn on the screen image!   */
  129. DrawImage(w->RPort,(struct Image *)&skl_img,1L,15L);
  130. if (argc==0) sv_st_item.Flags &= (~ITEMENABLED);
  131. SetMenuStrip(w, &menu);
  132.  
  133.    while( KeepGoing )
  134.    {
  135.       /* stay here until a message is received from Intuition */
  136.       Wait( 1L << w->UserPort->mp_SigBit);
  137.  
  138.       while(NewMessage=(IMSG)GetMsg(w->UserPort))  {
  139.       class = NewMessage->Class;
  140.       code = NewMessage->Code;
  141.       the_gadget = (struct Gadget *)NewMessage->IAddress;
  142.       ReplyMsg( NewMessage );
  143.  
  144.       /* enter switch on type of message received */
  145.       switch( class )
  146.       {
  147.          case CLOSEWINDOW:
  148.             goto get_out;
  149.             break;
  150.  
  151.          case MENUPICK:
  152.             menunumber = code;
  153.                itemnum = ITEMNUM(menunumber);
  154.                switch(itemnum) {
  155.  
  156.                   case MODE_IT:   /* Change Modes */
  157.                   mode = SUBNUM(menunumber);
  158.                   break;
  159.                      
  160.                   case LOAD_IT:   /* Load */
  161.                   if(modified) {
  162.                    oflg = AutoRequest(w,&ald_txt,&ayes_txt,
  163.                                  &no_txt,NULL,NULL,310L,60L);
  164.                    if(!oflg)
  165.                      goto skip1;
  166.                   }
  167.           /********************************************************/
  168.           /*  here's where you load a keymap                      */
  169.           /********************************************************/
  170.                   /*   if go, load it.   */
  171.                   if(go = get_name(w)) {
  172.                     if(LoadKeyMap(the_name)) 
  173.                         modified = FALSE;
  174.                     else {
  175.                         oflg = AutoRequest(w,&err_txt,NULL,
  176.                                  &no_txt,NULL,NULL,330L,60L);
  177.                     }
  178.                   }
  179.                   if(abort) goto error;
  180.                   skip1:
  181.                   break;
  182.  
  183.                   case SAVE_IT:   /* Save */
  184.                   /*   if go, save it.   */
  185.           /********************************************************/
  186.           /*  here's where you save a keymap                      */
  187.           /********************************************************/
  188.                   if(go = get_name(w)) {
  189.                     if(SaveKeyMap(the_name)) 
  190.                         modified = FALSE;
  191.                     else {
  192.                         oflg = AutoRequest(w,&err_txt,NULL,
  193.                                  &no_txt,NULL,NULL,330L,60L);
  194.                     }
  195.                   }
  196.                   if(abort) goto error;
  197.                   break;
  198.  
  199.                   case EXIT_OPT:   /* Quit */
  200.                   get_out:
  201.                   if(modified) {
  202.                    oflg = AutoRequest(w,&auto_txt,&yes_txt,
  203.                                  &no_txt,NULL,NULL,380L,60L);
  204.                    if(!oflg)
  205.                      goto skip;
  206.                   }
  207.                   if(class == CLOSEWINDOW) goto gone;
  208.                   subnum = SUBNUM(menunumber);
  209.                   if(subnum) {                /* Exit without setting */
  210.                     gone:
  211.                     KeepGoing = FALSE;
  212.                     break;
  213.                     }
  214.                   else {                      /* Exit & Set           */
  215.                     strcpy(buffer,"setmap ");
  216.                     strcat(buffer,filestring);
  217.                     Execute(buffer,0L,0L);
  218.                     KeepGoing = FALSE;
  219.           /********************************************************/
  220.           /*  here's where you setmap                             */
  221.           /********************************************************/
  222.                     break;
  223.                     }
  224.                   skip:
  225.                   break;
  226.  
  227.                   case CRED_IT:
  228.                    oflg = AutoRequest(w,&cred_txt,NULL,
  229.                                  &no_txt,NULL,NULL,500L,85L);
  230.                   break;
  231.  
  232.                   default:
  233.                   break;
  234.  
  235.                }
  236.             break;
  237.  
  238.          default:
  239.  
  240.             if((class == RAWKEY) && (code > 0x65)) 
  241.                 break;
  242.  
  243.          /*  Set which & rawkey                            */
  244.          /*  "which" corresponds to my gadget list index.  */
  245.          /*  rawkey corresponds to the Amiga keycode       */
  246.             if(class == GADGETDOWN) {
  247.                which = the_gadget->GadgetID;
  248.                rawkey = g_to_key[which];
  249.                }
  250.             if(class == RAWKEY) {
  251.                which = key_to_g[code];
  252.                rawkey = code;
  253.                }
  254.  
  255.     /* This test determines whether this is a qualifier key selected
  256.        or a "normal" key                                             */
  257.  
  258.          num1 = (LONG)gadget[which].UserData;
  259.          switch(num1) {
  260.    /*----------------------------------------------------------------------*/
  261.              case 0:
  262.  
  263.          /*  This is a normal key                          */
  264.  
  265.           /*  put ASCII keycode into text string for requester    */
  266.                sprintf(&code_buff[10],"%02.2x (hex)",rawkey);
  267.           /*  put qualifiers into text string for requester       */
  268.                sprintf(&qualtxt[11],"%s",&qual_name[qual_flag][0]);
  269.           /*  set name of key for requester                       */
  270.                name_text.IText = (UBYTE *)&name[which][0];
  271.  
  272.              if((which==89) || (which==60))
  273.                 return_key(w->RPort);
  274.              else
  275.                 fillup(w->RPort,which);
  276.  
  277.           /********************************************************/
  278.           /* here's where you copy key string text into &instring */
  279.           /********************************************************/
  280.  
  281.                String(rawkey);
  282.                strcpy(instring, DStr[qual_flag].string);
  283.  
  284.                if(mode) {
  285.           /*  set up the hex string buffer                        */
  286.                pos = stringinfo.BufferPos;
  287.                val = (SHORT)instring[pos];
  288.                sprintf(&hexval[0],"%02.2x",val);
  289.                hexinstring[0] = '\0';
  290.  
  291.           /*  turn on main requester                              */
  292.                Request(&main_requester, w);
  293.  
  294.                quit_sig = 0;
  295.  
  296.           /*  wait for the user to hit the gadgets                */
  297.  
  298.              do {
  299.                Wait( 1L << w->UserPort->mp_SigBit);
  300.  
  301.           /*  Get the message & copy the stuff.                   */
  302.                while(NewMessage=(IMSG)GetMsg(w->UserPort)) {
  303.                class = NewMessage->Class;
  304.                code = NewMessage->Code;
  305.                the_gadget = (struct Gadget *)NewMessage->IAddress;
  306.           /*  Reply to the message     */
  307.                ReplyMsg( NewMessage );
  308.                if (class==REQSET)
  309.                   success = ActivateGadget(&input_gadget,w,&main_requester);
  310.                else
  311.                   switch(the_gadget->GadgetID)
  312.                   {
  313.  
  314.               case STRING_ID:
  315.                  if(class == GADGETUP)
  316.                  {
  317.                     pos = stringinfo.BufferPos;
  318.                     if(pos == stringinfo.NumChars)
  319.                        instring[pos+1] = '\0';
  320.                     instring[pos] = 0x0d;
  321.                     stringinfo.BufferPos++;
  322.                     pos++;
  323.                     val = (SHORT)instring[pos];
  324.                     sprintf(&hexval[0],"%02.2x",val);
  325.                  }
  326.                  EndRequest(&main_requester, w);
  327.                  Request(&main_requester, w);
  328.                  break;
  329.  
  330.               case HEX_ID:
  331.                  if(class == GADGETDOWN)
  332.                  {
  333.                    pos = stringinfo.BufferPos;
  334.                    val = (SHORT)instring[pos];
  335.                    sprintf(&hexval[0],"%02.2x",val);
  336.                    hexinstring[0] = '\0';
  337.                  }
  338.                  else
  339.                  {
  340.                  /* Save current hex char at old pos          */
  341.                     if(pos == stringinfo.NumChars)
  342.                        instring[pos+1] = '\0';
  343.                     sscanf(&hexinstring[0],"%x",&val);
  344.                     strcpy(&hexval[0],&hexinstring[0]);
  345.                     instring[pos] = val;
  346.                  }
  347.                  RefreshGList(&quitgadget,w,&main_requester,-1L);
  348.                  break;
  349.  
  350.               case CANCEL_ID:  /*  This is the "cancel request" exit */
  351.                  quit_sig = 1;
  352.                  break;
  353.  
  354.               case QUIT_ID:    /*  This is the "Modify keymap" exit  */
  355.           /********************************************************/
  356.           /*  here's where you modify the keymap                  */
  357.           /********************************************************/
  358.  
  359.                  strcpy(DStr[qual_flag].string,instring);
  360.                  DStr[qual_flag].length = strlen(DStr[qual_flag].string);
  361.                  toolong=totlen=0;
  362.                  for(i=0;i<8;i++)
  363.                  {
  364.                     if(totlen>127)toolong=1;
  365.                     totlen+=DStr[i].length;
  366.                  }
  367.  
  368.                  modified = TRUE;
  369.                  if(toolong) 
  370.                  {
  371.                     EndRequest(&main_requester, w);
  372.                     Request(&main_requester, w);
  373.                     AutoRequest(w,&toolong_txt,NULL,&no_txt,NULL,NULL,400L,60L);
  374.                  }
  375.                  else 
  376.                  {
  377.                     quit_sig = 1;
  378.                     DeString(rawkey);
  379.                  }
  380.                  break;
  381.  
  382.               default:
  383.                  break;
  384.                }
  385.              }  /* End of message response while */
  386.              } while(!quit_sig);
  387.              EndRequest(&main_requester, w);
  388.              if(abort) goto error;
  389.              }
  390.              else {
  391.                 strcpy(&dstr_msg[24],name[which]);
  392.                 for (q=0;q<8;q++) {
  393.                     for(l=0;qual_name[q][l]!='\0';l++)
  394.                         kstr[q][l] = qual_name[q][l];
  395.  
  396.                     kstr[q][l++] = ' ';
  397.                     kstr[q][l++] = ' ';
  398.  
  399.                     for(k=0;DStr[q].string[k] != '\0';k++)
  400.                     {
  401.                        switch(DStr[q].string[k]) {
  402.                          case 0x0D:
  403.                              kstr[q][l++] = '<';
  404.                              kstr[q][l++] = 'C';
  405.                              kstr[q][l++] = 'R';
  406.                              kstr[q][l++] = '>';
  407.                              break;
  408.                             case 0x1F: case 0x1E: case 0x1D:
  409.                             case 0x1C: case 0x1B: case 0x1A: case 0x19:
  410.                          case 0x18: case 0x17: case 0x16: case 0x15:
  411.                          case 0x14: case 0x13: case 0x12: case 0x11:
  412.                          case 0x10: case 0x0F: case 0x0E:
  413.                          case 0x0C: case 0x0B: case 0x0A: case 0x09:
  414.                          case 0x08: case 0x07: case 0x06: case 0x05:
  415.                          case 0x04: case 0x03: case 0x02: case 0x01:
  416.                              kstr[q][l++] = 0x9B;
  417.                                break;
  418.                          default:
  419.                              kstr[q][l++] = DStr[q].string[k];
  420.                                 break;
  421.                    }
  422.                 }
  423.                 kstr[q][l] = '\0';
  424.                }
  425.                oflg = AutoRequest(w,&dstr,NULL,
  426.                                  &no_txt,NULL,NULL,640L,130L);
  427.  
  428.              }
  429.  
  430.              if((which==89) || (which==60))
  431.                 return_key(w->RPort);
  432.              else
  433.                 fillup(w->RPort,which);
  434.  
  435.    /*----------------------------------------------------------------------*/
  436.              break;             /*  end of "normal" key handler            */
  437.  
  438.          case 1:
  439.    /*    This is the qualifier handler.                */
  440.          switch (which) {
  441.  
  442.            case 47:   /*  Control */
  443.              qual_flag ^= KCF_CONTROL;
  444.              fillup(w->RPort, 47);
  445.              break;
  446.  
  447.            case 48:   /*  Shift   */
  448.            case 65:
  449.            case 76:
  450.              qual_flag ^= KCF_SHIFT;
  451.              fillup(w->RPort, 65);
  452.              fillup(w->RPort, 76);
  453.              fillup(w->RPort, 48);
  454.              break;
  455.  
  456.            case 81:   /*  alt     */
  457.            case 85:
  458.              qual_flag ^= KCF_ALT;
  459.              fillup(w->RPort, 81);
  460.              fillup(w->RPort, 85);
  461.              break;
  462.  
  463.            default:
  464.              break;
  465.          }
  466.    /*----------------------------------------------------------------------*/
  467.            break;                            /*  End of qualifier handler  */
  468.       }                                      /*  End of UserData switch    */
  469.     }                                        /*  End of type of message    */
  470.    }                                         /*  End of while messages     */
  471.    }                                         /*  End of "keep going"       */
  472.  
  473.    ClearStr(120);
  474.    FreeKeyMap(4);
  475.  
  476. error:
  477.    if(abort) Exit(103L);
  478.    ClearMenuStrip(w);
  479.    ConsoleCleanup();
  480.    CloseWindow(w);
  481.    CloseLibrary(GfxBase);
  482.    CloseLibrary(IntuitionBase);
  483. }
  484.  
  485.  
  486. fillup(rp, gadget_no)
  487. struct RastPort *rp;
  488. SHORT gadget_no;
  489. {
  490. long le,te,re,be;
  491.  
  492. SetAPen(rp, 3L);
  493. SetDrMd(rp,COMPLEMENT);
  494.  
  495.  le = l_edge[gadget_no];
  496.  te = KEYBASE+t_edge[gadget_no];
  497.  re = l_edge[gadget_no] + width[gadget_no] - 1;
  498.  be = te + 10;
  499.  RectFill(rp,le,te,re,be);
  500. }
  501.  
  502. return_key(rp)
  503. struct RastPort *rp;
  504. {
  505. SetAPen(rp, 0L);
  506. SetDrMd(rp,COMPLEMENT);
  507. RectFill(rp,439L,87L,479L,109L);
  508. RectFill(rp,417L,99L,438L,109L);
  509. }
  510.