home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / yak160src.lha / Yak_1.60_Src / settings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-18  |  10.4 KB  |  361 lines

  1.  /*
  2.  * Variables controlling Yak settings.
  3.  * Routines for initialisation at startup.
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <dos/dos.h>
  8. #include <dos/dosextens.h>
  9. #include <libraries/commodities.h>
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #include <string.h>
  13.  
  14. #include "yak.h"
  15. #include "hotkey_types.h"
  16. #include "gui.h"
  17. #define CATCOMP_NUMBERS
  18. #include "locale/yak_locale_strings.h"
  19.  
  20. #ifdef _SAS
  21. extern struct DosLibrary *DOSBase;
  22. #endif
  23.  
  24. /* local prototypes */
  25. static __regargs BOOL FWriteLong(BPTR fh, LONG n);
  26. static __regargs BOOL FReadLong(BPTR fh, LONG *n);
  27. static __regargs BOOL FWriteString(BPTR fh, char *buf);
  28. static __regargs BOOL FReadString(BPTR fh, char *buf, LONG len);
  29. static void LoadHotKeys(void);
  30. static void SaveHotKeys(void);
  31.  
  32. #define DEF_AUTOPOINT_DELAY      2
  33. LONG    autopoint_delay = DEF_AUTOPOINT_DELAY;  /* used for autopoint */
  34.  
  35. #define DEF_VOLUME      48
  36. LONG    click_volume = DEF_VOLUME;              /* used for keyclick */
  37.  
  38. #define DEF_BLANKSECS   300
  39. LONG    blanksecs = DEF_BLANKSECS;
  40. LONG    blanktimeout;
  41. LONG    blankcount;                             /* countdown to blank-time */
  42.  
  43. #define DEF_MBLANKSECS  5
  44. LONG    mouseblank = MB_SPRITES;
  45. LONG    mblanksecs = DEF_MBLANKSECS;
  46. LONG    mblanktimeout;
  47. LONG    mblankcount;                    /* countdown to mouse-blank-time */
  48.  
  49.  
  50. TOGGLEDATA toggles[] = {                /* -1 means UNUSED */
  51.         TRUE,   GDX_CTFCheck,           ROOT_WINDOW,
  52.         TRUE,   GDX_CTBCheck,           ROOT_WINDOW,
  53.         TRUE,   GDX_AutoCheck,          ROOT_WINDOW,
  54.         FALSE,  GDX_KeyActCheck,        ROOT_WINDOW,
  55.         TRUE,   GDX_ScrCycleCheck,      ROOT_WINDOW,
  56.         FALSE,  GDX_AutoPopCheck,       ROOT_WINDOW,
  57.         FALSE,  GDX_RMBActCheck,        ROOT_WINDOW,
  58.         FALSE,  -1,                     NO_WINDOW,
  59.         FALSE,  -1,                     NO_WINDOW,
  60.         FALSE,  GDX_WildStarCheck,      MISC_WINDOW,
  61.         TRUE,   GDX_ScrActCheck,        ROOT_WINDOW,
  62.         FALSE,  GDX_NoClickCheck,       MISC_WINDOW,
  63.         FALSE,  GDX_MMBActCheck,        ROOT_WINDOW,
  64.         FALSE,  GDX_BlackBorderCheck,   MISC_WINDOW,
  65.         TRUE,   GDX_BlankMouseOnKey,    BLANK_WINDOW,
  66.         FALSE,  GDX_MMBShiftCheck,      ROOT_WINDOW
  67. };
  68.  
  69. PATTERNDATA patterns[NUM_PATTERNS] = {
  70.         { "#?", NULL },                 /* autoactivation screens */
  71.         { "#?", NULL },                 /* click screens */
  72.         { "~(Workbench)", NULL },       /* autopop windows */
  73.         { "~(Workbench)", NULL }        /* click windows */
  74. };
  75.  
  76. /* 1.5 format
  77.  * Routines to load/save config file for Yak.
  78.  * Should handle config files in upward-compatible manner.
  79.  * File format:
  80.  *
  81.  *      LONG ID
  82.  *      LONG NUM_TOGGLES
  83.  *      BOOL toggles
  84.  *      LONG NUM_HOTKEYS                **REMOVED @ v1.5
  85.  *      STR hotkey1 '\n'                        :
  86.  *              :                               :
  87.  *      STR hotkeyN '\n'                        :
  88.  *      LONG NUM_PATTERNS
  89.  *      STR pattern1 '\n'
  90.  *              :
  91.  *      STR patternN '\n'
  92.  *      STR popcommand '\n'             **REMOVED @ v1.5
  93.  *      STR datefmt '\n'                **REMOVED @ v1.5
  94.  *      LONG click_volume, blanksecs
  95.  *      LONG mblanksecs                 **ADDED @ v1.3b
  96.  *      LONG mouseblank                 **ADDED @ v1.3e
  97.  */
  98.  
  99. #define CONFIG_ID       0x594b3135      /* YK15 */
  100.  
  101. /* write a LONG to a file (in binary format) - returns success*/
  102. static __regargs BOOL
  103. FWriteLong(BPTR fh, LONG n)
  104. {
  105.         return (BOOL)(FWrite(fh, (UBYTE *)&n, sizeof(LONG), 1) == 1);
  106. }
  107.  
  108. /* read a LONG to a file (in binary format) - returns success */
  109. static __regargs BOOL
  110. FReadLong(BPTR fh, LONG *n)
  111. {
  112.         return (BOOL)(FRead(fh, (UBYTE *)n, sizeof(LONG), 1) == 1);
  113. }
  114.  
  115. /* write a string to a file (in binary format) - returns success*/
  116. /* '\n' is appended */
  117. static __regargs BOOL
  118. FWriteString(BPTR fh, char *buf)
  119. {
  120.         FPuts(fh, buf);
  121.         FPutC(fh, '\n');
  122.         return (BOOL)(IoErr() == 0);
  123. }
  124.  
  125. /* read a string to a file (in binary format) - returns success */
  126. /* '\n' is stripped and buf null-terminated; assumes dest large enough */
  127. static __regargs BOOL
  128. FReadString(BPTR fh, char *buf, LONG len)
  129. {
  130.         FGets(fh, buf, len-1);
  131.         buf[strlen(buf)-1] = '\0';      /* '\n' --> '\0' */
  132.         return (BOOL)(IoErr() == 0);
  133. }
  134.  
  135. /* save current settings to config file */
  136. void
  137. SaveSettings()
  138. {
  139.     BPTR    fh;
  140.     UWORD   i;
  141.  
  142.     if (fh = Open(CONFIG_FILE, MODE_NEWFILE))
  143.     {
  144.         FWriteLong(fh, CONFIG_ID);
  145.  
  146.         /* toggles */
  147.         FWriteLong(fh, NUM_TOGGLES);
  148.         for (i = 0; i < NUM_TOGGLES; i++)
  149.             FWrite(fh, (UBYTE *)&toggles[i].pos, sizeof(BOOL), 1);
  150.  
  151.         /* patterns */
  152.         FWriteLong(fh, NUM_PATTERNS);
  153.         for (i = 0; i < NUM_PATTERNS; i++)
  154.             FWriteString(fh, patterns[i].patstr);
  155.  
  156.         /* miscellaneous */
  157.         FWriteLong(fh, click_volume);
  158.         FWriteLong(fh, blanksecs);
  159.         FWriteLong(fh, mblanksecs);
  160.         FWriteLong(fh, mouseblank);
  161.         FWriteLong(fh, autopoint_delay);
  162.  
  163.         Close(fh);
  164.     }
  165.  
  166.     SaveHotKeys();
  167. }
  168.  
  169.  
  170.  
  171. /* load current settings from file */
  172. void
  173. LoadSettings()
  174. {
  175.         BPTR    fh;
  176.         UWORD   i;
  177.         LONG    n;
  178.  
  179.         if (fh = Open(CONFIG_FILE, MODE_OLDFILE))
  180.         {
  181.                 FReadLong(fh, &n);
  182.                 if (n == CONFIG_ID)
  183.                 {
  184.                         /* toggles */
  185.                         FReadLong(fh, &n);
  186.                         for (i = 0; i < n; i++)
  187.                                 FRead(fh, (UBYTE *)&toggles[i].pos, sizeof(BOOL), 1);
  188.  
  189.                         /* patterns */
  190.                         FReadLong(fh, &n);
  191.                         for (i = 0; i < n; i++)
  192.                                 FReadString(fh, patterns[i].patstr, PATLEN+1);
  193.  
  194.                         /* miscellaneous */
  195.                         FReadLong(fh, &click_volume);
  196.                         FReadLong(fh, &blanksecs);
  197.                         if (!FReadLong(fh, &mblanksecs))        /* none there */
  198.                                 mblanksecs = DEF_MBLANKSECS;
  199.                         if (!FReadLong(fh, &mouseblank))        /* none there */
  200.                                 mouseblank = MB_SPRITES;
  201.                         if (!FReadLong(fh, &autopoint_delay))   /* none there */
  202.                                 autopoint_delay = DEF_AUTOPOINT_DELAY;
  203.                 }
  204.                 else PostError(getString(Invalid_config_file_ERR));
  205.                 Close(fh);
  206.         }
  207.         DeleteYakHotKeyList();          /* delete old keys */
  208.         LoadHotKeys();                  /* and load new ones */
  209.  
  210.         /* set-up patterns */
  211.         for (i = 0; i < NUM_PATTERNS; i++)
  212.                 InitPattern(NULL, i);
  213.  
  214.         if (wildstar)
  215.                 WILDSTARON;
  216.         else
  217.                 wildstar = ((struct RootNode *)(((struct DosLibrary *)DOSBase)->dl_Root))->rn_Flags & RNF_WILDSTAR;
  218.  
  219.                 
  220.         if (noclick) SetClickDrive(noclick);
  221.         
  222.         if (blackborder) ToggleBlackBorder(blackborder);
  223.  
  224.         if (mmbshift) ToggleMMBShift(mmbshift);
  225.  
  226.         blankcount = blanktimeout = 10*blanksecs;
  227.         mblankcount = mblanktimeout = 10*mblanksecs;
  228. }
  229.  
  230. /*
  231.  *      HOTKEY FILE FORMAT
  232.  *
  233.  *      LONG    ID      'YKK1'
  234.  *      LONG NUM_HOTKEYS
  235.  *      UWORD type1
  236.  *      UWORD opts1
  237.  *      STR hotkey1 '\n'
  238.  *      STR argstr1 '\n'
  239.  *              :
  240.  *      STR hotkeyN '\n'
  241.  */
  242. #define HOTKEY_ID 0x594B4B31    /* 'YKK1' */
  243.  
  244. static void
  245. SaveHotKeys()
  246. {
  247.     YakHotKey *yhk;
  248.     BPTR    fh;
  249.     UWORD   type, i;
  250.         
  251.     if (fh = Open(HOTKEY_FILE, MODE_NEWFILE))
  252.     {
  253.         FWriteLong(fh, HOTKEY_ID);
  254.                 
  255.         FWriteLong(fh, num_hkeys);
  256.                 
  257.         for (type = 0; type < NUM_HOTKEY_TYPES; type++)
  258.             for (yhk = (YakHotKey *)keylist(type)->lh_Head, i = 0;
  259.                  i < numkeys(type);
  260.                  i++, yhk = (YakHotKey *)yhk->yhk_Node.ln_Succ)
  261.             {
  262.                 FWrite(fh, (UBYTE *)&yhk->yhk_Type, sizeof(UWORD), 1);
  263.                 FWrite(fh, (UBYTE *)&yhk->yhk_Options, sizeof(UWORD), 1);
  264.                 FWriteString(fh, yhk->yhk_KeyDef);
  265.                 FWriteString(fh, yhk->yhk_ArgStr ? yhk->yhk_ArgStr : "");
  266.             }
  267.  
  268.         Close(fh);
  269.     }
  270. }
  271.  
  272. static void
  273. LoadHotKeys()
  274. {
  275.     YakHotKey *yhk;
  276.     char    *buf;
  277.     UWORD   type, opts;
  278.     BPTR    fh;
  279.     LONG    n;
  280.     UWORD   i;
  281.  
  282.  
  283.     DEBUG_BEGIN("LoadHotKeys()");
  284.  
  285.     if (!(buf = AllocVec(512, 0L)))
  286.     {
  287.         PostError(getString(No_memory_ERR));
  288.         return;
  289.     }
  290.         
  291.     if (fh = Open(HOTKEY_FILE, MODE_OLDFILE))
  292.     {
  293.         FReadLong(fh, &n);
  294.                 
  295.         if (n == HOTKEY_ID)
  296.         {
  297.             FReadLong(fh, &n);
  298.  
  299.             DEBUG_PRINTF("  n = %d\n",n);
  300.  
  301.             for (i = 0; i < n; i++)
  302.             {
  303.  
  304.                 DEBUG_PRINTF("  i = %d\n",i);
  305.  
  306.                 if (FRead(fh, (UBYTE *)&type, sizeof(UWORD), 1) && 
  307.                     FRead(fh, (UBYTE *)&opts, sizeof(UWORD), 1))
  308.                 {               
  309.  
  310.                     DEBUG_PRINTF("  type = %d\n",type);
  311.                     DEBUG_PRINTF("  opts = %d\n",opts);
  312.  
  313.                     if ((type >= NUM_HOTKEY_TYPES) || (type < 0))
  314.                     {
  315.                         /* ignore key definition */
  316.                         FReadString(fh, buf, 512);
  317.                         FReadString(fh, buf, 512);
  318.                         continue;
  319.                     }
  320.                     else
  321.                     {
  322.                         if (yhk = NewYakHotKey(type))
  323.                         {
  324.                             yhk->yhk_Options = opts;
  325.                             FReadString(fh, buf, 512);
  326.  
  327.                             DEBUG_PRINTF("  buf = %s\n",buf);
  328.  
  329.                             if (ModifyYHKKeyDef(yhk, buf))
  330.                             {
  331.  
  332.                                 FReadString(fh, buf, 512);
  333.                                 if (!buf[0] || ModifyYHKArgStr(yhk, buf))
  334.                                 {
  335.                                     continue;
  336.                                 }
  337.                             }
  338.                         }       /* end if (yhk = NewYakHotKey(type)) */
  339.  
  340.                     }           /* end else ((type >= NUM_HOTKEY_TYPES) || (type < 0)) */
  341.  
  342.                 }               /* end if FRead (opts, type) */
  343.         
  344.             }                   /* end for */
  345.         }
  346.         else
  347.         {
  348.             PostError(getString(Error_reading_hotkey_file_ERR));
  349.  
  350.         }                       /* end if (n == HOTKEY_ID) */
  351.         
  352.  
  353.         DEBUG_PRINTF("num_hkeys = %d\n",num_hkeys);
  354.  
  355.         Close(fh);
  356.  
  357.     }                           /* end if fh */
  358.  
  359.     FreeVec(buf);
  360. }
  361.