home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 266.lha / SetKey_v2.0 / src / showsubs.c < prev    next >
C/C++ Source or Header  |  1989-07-10  |  10KB  |  462 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. extern UBYTE filestring[];
  17.  
  18. UBYTE *LoTypes;
  19. ULONG *LoMap;
  20. UBYTE *LoCap;
  21. UBYTE *LoRep;
  22. UBYTE *HiTypes;
  23. ULONG *HiMap;
  24. UBYTE *HiCap;
  25. UBYTE *HiRep;
  26. int abort=0;
  27.  
  28. extern void FillKeyMap();
  29. extern long DLength();
  30. extern void NewStr();
  31.  
  32. long tlen;
  33. UBYTE *told,*tnew;
  34.  
  35. struct {
  36. UBYTE length;
  37. UBYTE string[127];
  38. } DStr[8]; /* one per each qualifier */
  39.  
  40. struct IOStdReq *WriteMsg;   /* I/O request block pointer */
  41. struct MsgPort  *WritePort;  /* a port at which to receive replies*/
  42. int DescLen[8]={2,4,4,8,4,8,8,16};
  43. struct KeyMap MyKeyMap;
  44.  
  45. /*      For writing to the console: */
  46. void MakeConWP()
  47. {
  48.    WritePort = CreatePort("mycon.write",0L);
  49.    if(WritePort == 0L) exit(100);    /* error in createport */
  50.    WriteMsg =  CreateStdIO(WritePort);
  51.    if(WriteMsg == 0L) exit(200);     /* error in createstdio */
  52. }
  53.  
  54.  
  55.  
  56. /* output a NULL-terminated string of characters to a console */
  57. void
  58. TurnCursorOff()
  59. {
  60.    WriteMsg->io_Command = CMD_WRITE;
  61.    WriteMsg->io_Data = (APTR)" p";
  62.    WriteMsg->io_Length = -1;
  63.    DoIO(WriteMsg);
  64.    return;
  65. }
  66.  
  67.  
  68.  
  69.  
  70. /* this function returns a value of 0 if the console
  71.  * device opened correctly and a nonzero value (the error
  72.  * returned from OpenDevice) if there was an error.
  73.  */
  74. long OpenConsole(window)
  75. struct Window *window;
  76. {
  77. /* Open a console device */
  78.    long error;
  79.  
  80.    MakeConWP(); /* make a write console port */
  81.    WriteMsg->io_Data   = (APTR) window;
  82.    WriteMsg->io_Length = (long)sizeof(*window);
  83.    error=OpenDevice("console.device", 0L, WriteMsg, 0L);
  84.    TurnCursorOff();
  85.    return(error);
  86. }
  87.  
  88.  
  89.  
  90. /* Set the current KeyMap with pointers in given keymap. */
  91.  
  92. void SetKeyMap(KeyMap)
  93. struct KeyMap *KeyMap;
  94. {
  95.    WriteMsg->io_Command = CD_SETKEYMAP;
  96.    WriteMsg->io_Length = 32L;
  97.    WriteMsg->io_Data = (APTR)KeyMap;
  98.    DoIO(WriteMsg);
  99. }
  100.  
  101.  
  102. /* Make a new string so the FreeMem does not crash */
  103. void FixDeadKey(key)
  104. int key;
  105. {
  106.    int i;
  107.    char *temp;
  108.  
  109.    told=(UBYTE *)LoMap[key];
  110.    tnew=(UBYTE *)&LoMap[key];
  111.    LoMap[key]=0;
  112.  
  113.    if(told[0]==0) tnew[3]=told[1];
  114.    else tnew[3]=*(told+told[1]);
  115.  
  116.    if(told[2]==0) tnew[2]=told[3];
  117.    else tnew[2]=*(told+told[3]);
  118.  
  119.    LoTypes[key]&=0x0F;
  120. }
  121.  
  122.  
  123.  
  124. /* Fill Default keymap and copy to MyKeyMap */
  125. void DefaultKeyMap()
  126. {
  127.    int i;
  128.  
  129.    FillKeyMap(&MyKeyMap);
  130.    for(i=0;i<64;i++)
  131.    {
  132.       LoTypes[i]=MyKeyMap.km_LoKeyMapTypes[i];
  133.       LoMap[i]=MyKeyMap.km_LoKeyMap[i];
  134.       if(LoTypes[i]&0x40) NewStr(i);
  135.       if(abort) return;
  136.       if(LoTypes[i]&0x20) FixDeadKey(i);
  137.    }
  138.    for(i=0;i<8;i++)
  139.    {
  140.       LoCap[i]=MyKeyMap.km_LoCapsable[i];
  141.       LoRep[i]=MyKeyMap.km_LoRepeatable[i];
  142.    }
  143.    for(i=0;i<56;i++)
  144.    {
  145.       HiTypes[i]=MyKeyMap.km_HiKeyMapTypes[i];
  146.       HiMap[i]=MyKeyMap.km_HiKeyMap[i];
  147.       if(HiTypes[i]&0x40) NewStr(i+0x40);
  148.       if(abort) return;
  149.       if(HiTypes[i]&0x20) FixDeadKey(i+0x40);
  150.    }
  151.    for(i=0;i<7;i++)
  152.    {
  153.       HiCap[i]=MyKeyMap.km_HiCapsable[i];
  154.       HiRep[i]=MyKeyMap.km_HiRepeatable[i];
  155.    }
  156. }
  157.  
  158.  
  159.  
  160. /* DeAlloc string mem before exit and before LoadKeyMap() */
  161. void ClearStr(key)
  162. int key;
  163. {
  164.    int i;
  165.    for(i=0;i<key;i++) /* deallocate strings both lo and hi*/
  166.    {
  167.       if(LoTypes[i]&0x40)
  168.       {
  169.          told=(UBYTE *)LoMap[i];
  170.          FreeMem(told,DLength(told));
  171.       }
  172.    }
  173. }
  174.  
  175.  
  176.  
  177. ULONG LoadKeyMap(string)
  178. char *string;
  179. {
  180.    int i;
  181.    ULONG segment;
  182.    UWORD *Ptr,*km;
  183.    ULONG *temp,*nptr,length;
  184.    char *name;
  185.  
  186.    segment=(ULONG)LoadSeg(string);
  187.    if(segment==0) return(0L);
  188.  
  189.    Ptr=(UWORD *)BADDR(segment);
  190.    temp=(ULONG *)Ptr;
  191.    length=*(temp-1);
  192.    nptr=(ULONG *)(Ptr+7);
  193.    name=(char *)*nptr;
  194.  
  195.    if((ULONG)name < (ULONG)temp || (ULONG)name > (length+(ULONG)temp))
  196.    {
  197.       UnLoadSeg(segment);
  198.       return(0L);
  199.    }
  200.    km=(UWORD *)&MyKeyMap.km_LoKeyMapTypes;
  201.    for(i=0;i<16;i++) km[i]=Ptr[i+9];
  202.    SetKeyMap(&MyKeyMap);
  203.    ClearStr();
  204.    DefaultKeyMap();
  205.  
  206.    UnLoadSeg(segment);
  207.    return(segment);
  208. }
  209.  
  210.  
  211.  
  212. /* Fill the given KeyMap with pointer to current keymap. */
  213. void FillKeyMap(KeyMap)
  214. struct KeyMap *KeyMap;
  215. {
  216.    WriteMsg->io_Command = CD_ASKKEYMAP;
  217.    WriteMsg->io_Length = 32L;
  218.    WriteMsg->io_Data = (APTR) KeyMap;
  219.    DoIO(WriteMsg);
  220. }
  221.  
  222.  
  223. /* strings are stored as sl0sO0sl1sO1...s0s1...
  224.  * where sl0 is length of string 0 and sO0 is offset from start of descriptor
  225.  * since descriptor overhead comes first sO0 is number of bytes in descriptor
  226.  * since contiguous and monotonically increasing the last string offset +
  227.  * length is the total length.
  228.  */
  229. long DLength(DString)
  230. UBYTE *DString;
  231. {
  232.    long temp;
  233.    int i,num;
  234.    temp=num=DString[1];   /* number of strings in descriptor */
  235.    for (i=0;i<num;i++,i++) temp+=DString[i];  /* total length of strings */
  236.    return(temp);
  237. }
  238.  
  239.  
  240. FreeKeyMap(which)
  241. int which;
  242. {
  243.    FreeMem(LoTypes,120L);
  244.    if(which>1)FreeMem(LoMap,480L);
  245.    if(which>2)FreeMem(LoCap,15L);
  246.    if(which>3)FreeMem(LoRep,15L);
  247. }
  248.  
  249.  
  250. /* Make a new string so the FreeMem does not crash */
  251. void NewStr(key)
  252. int key;
  253. {
  254.    int i,j,in,out,len,desc;
  255.    UBYTE type;
  256.  
  257.    told=(UBYTE *)LoMap[key];
  258.    tlen=DLength(told);       /* total length of strings + descriptor  */
  259.    tnew=(UBYTE *)AllocMem(tlen,0L);
  260.    if(tnew==0)
  261.    {
  262.       ClearStr(key);
  263.       FreeKeyMap(4);
  264.       abort=1;
  265.       return;
  266.    }
  267.  
  268.    type=LoTypes[key]&0x07;
  269.    desc=out=DescLen[type]; /* account for bytes in descriptor */
  270.    for(i=0;i<desc;i++)
  271.    {
  272.       len=tnew[i]=told[i];
  273.       i++;
  274.       in=told[i];
  275.       tnew[i]=out;
  276.       for(j=0;j<len;j++)tnew[out+j]=told[in+j];
  277.       out+=len;
  278.    }
  279.    LoMap[key]=(ULONG)tnew;
  280. }
  281.  
  282.  
  283.  
  284.  
  285.  
  286. void ConsoleCleanup()
  287. {
  288.    CloseDevice(WriteMsg);
  289.    DeleteStdIO(WriteMsg);
  290.    DeletePort(WritePort);
  291. }
  292.  
  293.  
  294.  
  295. /* take string and put in DStr array for easy editing */
  296. void String(key)
  297. int key;
  298. {
  299.    int i,in,out,test,oldtest,offset;
  300.    UBYTE type;
  301.  
  302.    for (i=0;i<8;i++) DStr[i].length=DStr[i].string[0]=NULL;
  303.    type=LoTypes[key]&0x07;
  304.    if (LoTypes[key]&0x40) /* strings */
  305.    {
  306.       told=(UBYTE *)LoMap[key];
  307.       oldtest=-1;
  308.       in=0;
  309.       for(out=0;out<8;out++)
  310.       {
  311.          if ((test=(type&out))>oldtest)
  312.          {
  313.             oldtest=test;
  314.             DStr[out].length=told[(in<<1)];  /* length */
  315.             offset=told[(in<<1)+1];          /* offset */
  316.             for(i=0;i<DStr[out].length;i++)DStr[out].string[i]=told[offset+i];
  317.             DStr[out].string[i]=NULL;
  318.             in++;
  319.          }
  320.       }
  321.    }
  322.    else /* plain keys */
  323.    {
  324.       told=(UBYTE *)&LoMap[key];
  325.       if((type&7)==7) type=3; /* special case of ctrl with bits 5&6 to 0 */
  326.       oldtest=-1;
  327.       in=3;
  328.       for(out=0;out<8;out++)
  329.       {
  330.          if ((test=(type&out))>oldtest)
  331.          {
  332.             oldtest=test;
  333.             DStr[out].length=1;            /* length    */
  334.             DStr[out].string[0]=told[in];  /* key value */
  335.             DStr[out].string[1]=NULL;
  336.             --in;
  337.          }
  338.       }
  339.    }
  340. } /* end of String */
  341.  
  342.  
  343.  
  344.  
  345. /* put string back in standard Descriptor format from DStr array */
  346. DeString(key)
  347. int key;
  348. {
  349.    int shift,alt,ctrl,i,offset,keyflag,sum,test,oldtest,in,out;
  350.    UBYTE type;
  351.  
  352.    shift =DStr[1].length+DStr[3].length+DStr[5].length+DStr[7].length;
  353.    alt   =DStr[2].length+DStr[3].length+DStr[6].length+DStr[7].length;
  354.    ctrl  =DStr[4].length+DStr[5].length+DStr[6].length+DStr[7].length;
  355.    type=0;
  356.    if (shift) type|=1;
  357.    if (alt)   type|=2;
  358.    if (ctrl)  type|=4;
  359.    offset=tlen=DescLen[type]; /* account for bytes in descriptor */
  360.  
  361.    keyflag=1; /* true mean it is a key */
  362.    for (sum=0,i=0;i<8;i++)
  363.    {
  364.       if (DStr[i].length>1) keyflag=0;
  365.       sum+=DStr[i].length;
  366.    }
  367.    if (sum>4) keyflag=0;
  368.    
  369.    if (!keyflag) /* strings */
  370.    {
  371.       for (i=0;i<8;i++) tlen+=DStr[i].length;
  372.       tnew=AllocMem(tlen,0L);
  373.       if(tnew==0)
  374.       {
  375.          ClearStr(120);
  376.          FreeKeyMap(4);
  377.          abort=1;
  378.          return;
  379.       }
  380.       if (LoTypes[key]&0x40)
  381.       {
  382.          told=(UBYTE *)LoMap[key];
  383.          FreeMem(told,DLength(told));
  384.       }
  385.  
  386.       oldtest=-1;
  387.       in=0;
  388.       for(out=0;out<8;out++)
  389.       {
  390.          if ((test=(type&out))>oldtest)
  391.          {
  392.             oldtest=test;
  393.             tnew[(in<<1)]=DStr[out].length;  /* length */
  394.             tnew[(in<<1)+1]=offset;          /* offset */
  395.             for(i=0;i<DStr[out].length;i++)tnew[offset+i]=DStr[out].string[i];
  396.             offset+=DStr[out].length;
  397.             in++;
  398.          }
  399.       }
  400.       LoMap[key]=(ULONG)tnew;
  401.       LoTypes[key]=0x40|type;
  402.    }
  403.    else /* plain keys */
  404.    {
  405.       told=(UBYTE *)&LoMap[key];
  406.       oldtest=-1;
  407.       in=3;
  408.       for(out=0;out<8;out++)
  409.       {
  410.          if ((test=(type&out))>oldtest)
  411.          {
  412.             oldtest=test;
  413.             told[in]=DStr[out].string[0];  /* key value */
  414.             --in;
  415.          }
  416.       }
  417.       if (type!=3 || LoTypes[key]!=7) LoTypes[key]=type;
  418.    }
  419. }
  420.  
  421.  
  422. /* Save off current keymap in hunk format (not easy)*/
  423. ULONG
  424. SaveKeyMap(strng)
  425. char *strng;
  426. {
  427.    return(1L);
  428. }
  429.  
  430. AllocKeyMap()
  431. {
  432.    LoTypes=(UBYTE *)AllocMem(120L,0L);
  433.    if(LoTypes==0) goto Cancel;
  434.    LoMap=  (ULONG *)AllocMem(480L,0L);
  435.    if(LoMap==0) 
  436.    {
  437.       FreeKeyMap(1);
  438.       goto Cancel;
  439.    }
  440.    LoCap=  (UBYTE *)AllocMem(15L,0L);
  441.    if(LoCap==0) 
  442.    {
  443.       FreeKeyMap(2);
  444.       goto Cancel;
  445.    }
  446.    LoRep=  (UBYTE *)AllocMem(15L,0L);
  447.    if(LoRep==0) 
  448.    {
  449.       FreeKeyMap(3);
  450.       goto Cancel;
  451.    }
  452.  
  453.    HiTypes=LoTypes+64;
  454.    HiMap=  LoMap+64;
  455.    HiCap=  LoCap+8;
  456.    HiRep=  LoRep+8;
  457.    return;
  458. Cancel:
  459.    abort=1;
  460.    return;
  461. }
  462.