home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gemlib27.lzh / GEMLIB27 / WINIO.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  12KB  |  505 lines

  1. /*
  2.  * This file contains file io routines
  3.  */
  4. #ifdef __GNUC__
  5. #  include <gemfast.h>
  6. #  include <aesbind.h>
  7. #  include <vdibind.h>
  8. #else
  9. #  include <obdefs.h>
  10. #  include <gemdefs.h>
  11. #  include <xbios.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <osbind.h>
  15. #include "wind.h"
  16. #include "uw.h"
  17. #include "windefs.h"
  18.  
  19. extern struct wi_str w[];
  20.  
  21. /*
  22.  * Character stuff (fonts, keymaps).
  23.  */
  24. extern FNT *fnttbl[10];            /* List of pointers to fonts avail */
  25. extern int fontsavail;            /* Number of fonts available */
  26. extern FNT *curfont;            /* font in use */
  27. extern int fontmenuobj[];        /* array of font menu object numbers */
  28. /*
  29.  * Options (somewhat loosely defined).
  30.  */
  31. extern int fast;            /* flag for fast open/close */
  32. extern int overstrike;            /* flag for character output */
  33. extern int sliders;            /* flag for sliders on new windows */
  34. extern int titles;            /* flag for title bars on new windows */
  35. extern int audibell;            /* Audible bell. */
  36. extern int visibell;            /* Visible bell. */
  37. extern int toponbel;            /* Top window on bell. */
  38.  
  39. extern FUNCSTRING fstrings[];        /* storage for function key bodys */
  40.  
  41. extern char cmdpath[];            /* default command executions strings */
  42. extern char cmdname[];
  43. extern char cmdargs[];
  44.  
  45. extern OBJECT *menubar;
  46.  
  47. void rsbufset();
  48. long    bufsizs[] = {        /* rs-232 buffer sizes setable from uw */
  49.     256l,    2048l,    16384l
  50. };
  51. int    bufsiz = 0;        /* index into table above of current size */
  52.  
  53. #ifdef    LOCATE
  54. extern    char    *locate();
  55. #else
  56. #define    locate(n)    n
  57. #endif
  58.  
  59. /* read a font from a file */
  60. FNT * loadfont (name)
  61. char    *name;
  62. {
  63.   FNT * curfont;
  64.   char *tmpfnt;
  65.   char menustr[20];
  66.   char namestr[20];
  67.   register int shift;
  68.   register unsigned int mask;
  69.   register int    i;
  70.   char *getmem();
  71.  
  72.   if ((curfont = (FNT *) getmem((long)sizeof(FNT))) == NULL)
  73.     return(NULL);
  74.   i = Fopen(locate(name), 0);
  75.   if (i<0)
  76.     return (NULL);
  77.   Fread(i, 2048L, curfont->f_data);
  78.   Fclose(i);
  79.   curfont->inc_x = MINMAX(curfont->f_data[16], 1, 8);
  80.   curfont->inc_y = MINMAX(curfont->f_data[32], 1, 16);
  81.   shift = 8-curfont->inc_x;
  82.   mask = (1<<curfont->inc_x)-1;
  83.   tmpfnt = curfont->f_data;
  84.   for (i=0; i<2048; i++)
  85.   {
  86.     *tmpfnt = (*tmpfnt>>shift)&mask;
  87.     tmpfnt++;
  88.   }
  89.   sscanf(name, "wind%3s", namestr);
  90.   sprintf(menustr, "%1.2d x %1.2d %s font", curfont->inc_x, curfont->inc_y,
  91.     namestr);
  92.   set_menu_string(menustr, fontmenuobj[fontsavail]);
  93.   gen_hash(curfont, &curfont->f_hash);
  94.  
  95.   fnttbl[fontsavail] = curfont;
  96.   fontsavail++;
  97.   return (curfont);
  98. }
  99.  
  100. /* This function reads the config file from name in directory path and sets
  101.  * the apropriate variables.
  102.  */
  103. read_config(path, name)
  104. char *path;
  105. char *name;
  106. {
  107.   char file[100];
  108.   char *ptr;
  109.   char *status;
  110.   FILE *fd;
  111.   int i, j;
  112.   char *index(), *rindex();
  113.   
  114.   strcpy(file, path);
  115.   ptr = rindex(file, '\\');
  116.   if (ptr != NULL)
  117.     *(ptr+1) = '\0';
  118.   strcat(file, name);
  119.   
  120.   fd = fopen(file, "r");
  121.   if (fd == NULL)
  122.   {
  123.     if (*path != '\0')    /* don't print error if in startup */
  124.       form_alert(1, "[2][Can't open config input file][ok]");
  125.     return;
  126.   }
  127.   status = fgets(file, 100,fd);
  128.   while (status != NULL)
  129.   {
  130.     char * endline;
  131.     if ((endline = rindex(file, '\n')) != NULL)
  132.       *endline = '\0';
  133.     if (!strncmp("audibell=", file, 9L))
  134.       audibell = atoi(file + 9);
  135.     else if (!strncmp("visibell=", file, 9L))
  136.       visibell = atoi(file + 9);
  137.     else if (!strncmp("topbell=", file, 8L))
  138.       toponbel = atoi(file + 8);
  139.     else if (!strncmp("fast", file, 4L))
  140.       ++fast;
  141.     else if (!strncmp("slow", file, 4L))
  142.         fast = 0;
  143.     else if (!strncmp("overstrike", file, 4L))
  144.       ++overstrike;
  145.     else if (!strncmp("nooverstrike", file, 4L))
  146.       overstrike = 0;
  147.     else if (!strncmp("sliders", file, 7L))
  148.       ++sliders;
  149.     else if (!strncmp("nosliders", file, 9L))
  150.         sliders = 0;
  151.     else if (!strncmp("titles", file, 6L))
  152.       ++titles;
  153.     else if (!strncmp("notitless", file, 8L))
  154.         titles = 0;
  155.     else if (!strncmp("font=", file, 5L))
  156.     {
  157.       i = atoi(file+5);
  158.       if (fontsavail > i)
  159.       {
  160.         for (j = 0; j < fontsavail; j++)
  161.           objc_change(menubar, fontmenuobj[j], 0, 0, 0, 0, 0, NONE, 0);
  162.         objc_change(menubar, fontmenuobj[i], 0, 0, 0, 0, 0, CHECKED, 0);
  163.         curfont = fnttbl[i];
  164.       }
  165.     }
  166.     else if (!strncmp("bufsiz=", file, 7L))
  167.     {
  168.       i = atoi(file+7);
  169.       for (j=0; j < (int)(sizeof(bufsizs)/sizeof(bufsizs[0])); j++)
  170.           if (bufsizs[j] >= i)
  171.       break;
  172.       bufsiz = j;
  173.       rsbufset(j);
  174.     }
  175.     else if (file[0] == 'f' && file[1] >= '0' && file[1] <= '9'
  176.       && (file[2] == '=' || file[3] == '='))
  177.       {
  178.     i = atoi(file+1);
  179.     if (i < 1) i = 1;
  180.     if (i > NFSTRINGS - 2) i = NFSTRINGS - 2;
  181.     status = index(file, '=');
  182.     ptr = index(status, '\n');
  183.     if (ptr != NULL)
  184.       *ptr = '\0';
  185.     strcpy(fstrings[i-1], status+1);
  186.       }
  187.     else if (!strncmp("cmdpath=", file, 8L))
  188.     strcpy(cmdpath, file+8);
  189.     else if (!strncmp("cmdname=", file, 8L))
  190.     strcpy(cmdname, file+8);
  191.     else if (!strncmp("cmdargs=", file, 8L))
  192.     strcpy(cmdargs, file+8);
  193.     status = fgets(file, 100, fd);
  194.   }
  195.   objc_change(menubar, VISIBELL, 0, 0, 0, 0, 0,
  196.     visibell? CHECKED: 0, 0);
  197.   objc_change(menubar, AUDIBELL, 0, 0, 0, 0, 0,
  198.     audibell? CHECKED: 0, 0);
  199.   objc_change(menubar, TOPONBEL, 0, 0, 0, 0, 0,
  200.     toponbel? CHECKED: 0, 0);
  201.   menu_icheck(menubar, MFAST, !fast);
  202.   menu_icheck(menubar, OVERSTRI, overstrike);
  203.   menu_icheck(menubar, WINSTYLE, sliders);
  204.   menu_icheck(menubar, WINTITLE, titles);
  205. }
  206.  
  207. /* This function writes the config file name in directory path.
  208.  */
  209. write_config (path, name)
  210. char    *path;
  211. char    *name;
  212. {
  213.     char    file[100];
  214.     char    *ptr;
  215.     FILE    *fd;
  216.     int    i;
  217.     char    *rindex();
  218.   
  219.     strcpy(file, path);
  220.     ptr = rindex(file, '\\');
  221.     if (ptr != NULL)
  222.         *(ptr + 1) = '\0';
  223.     strcat(file, name);
  224.   
  225.     fd = fopen(file, "w");
  226.     if (fd == NULL)
  227.     {
  228.         form_alert(1, "[2][Can't open config output file][ok]");
  229.         return;
  230.     }
  231.  
  232.     fprintf(fd, "audibell=%d\n", audibell);
  233.     fprintf(fd, "visibell=%d\n", visibell);
  234.     fprintf(fd, "topbell=%d\n", toponbel);
  235.     fprintf(fd, "%s\n", fast? "fast": "slow");
  236.     fprintf(fd, "%s\n", overstrike? "overstrike": "nooverstrike");
  237.     fprintf(fd, "%s\n", sliders? "sliders": "nosliders");
  238.     fprintf(fd, "%s\n", titles? "titles": "notitles");
  239.     fprintf(fd, "bufsiz=%ld\n", bufsizs[bufsiz]);
  240.     fprintf(fd, "cmdpath=%s\n", cmdpath);
  241.     fprintf(fd, "cmdname=%s\n", cmdname);
  242.     fprintf(fd, "cmdargs=%s\n", cmdargs);
  243.     for (i = 0; i < fontsavail; i++)
  244.         if (curfont == fnttbl[i])
  245.             fprintf(fd, "font=%d\n", i);
  246.  
  247.     for (i = 0; i < NFSTRINGS - 2; ++i)
  248.         fprintf(fd, "f%d=%s\n", i + 1, fstrings[i]);
  249.     fclose(fd);
  250. }
  251.  
  252. setcapture (wp)
  253. WI_STR    *wp;
  254. {
  255.     char    full[80];
  256.     register char    *cp1, *cp2;
  257.  
  258.     if (wp->wi_lfd)
  259.     {
  260.         fclose(wp->wi_lfd);
  261.         wp->wi_lfd = NULL;
  262.     }
  263.     if (*wp->wi_fname)
  264.     {
  265.         cp1 = full;
  266.         for (cp2 = wp->wi_fpath; *cp2; *cp1++ = *cp2++);
  267.         while (--cp1 >= full && *cp1 != '\\');
  268.         strcpy(++cp1, wp->wi_fname);
  269.         if ((wp->wi_lfd = fopen(full, "a")) == NULL)
  270.         {
  271.             form_alert(1, "[2][Unable to open capture file.][ok]");
  272.             *wp->wi_fname = '\0';
  273.         }
  274.     }
  275.     w_rename(wp - w, NULL);
  276. }
  277.  
  278. #define    DEFUCR    0x88        /* Async, 1 stop bit */
  279.  
  280. int    speedobjs[] = {
  281.     BB1920,    BB960,    BB480,    BB360,    BB240,    BB200,    BB180,    BB120,
  282.     BB60,    BB30,    BB20,    BB15,    BB13,    BB11,    BB7,    BB5
  283. };
  284. int    flowobjs[] = {
  285.     FCBNONE,    FCBXON,        FCBRTS
  286. };
  287. int    parobjs[] = {
  288.     PBNONE,        PBODD,        PBEVEN
  289. };
  290. int    stopobjs[] = {
  291.     SBB1,        SBB15,        SBB2
  292. };
  293. int    bcbobjs[] = {
  294.     BCB8,        BCB7,        BCB6,        BCB5
  295. };
  296. int    bufobjs[] = {
  297.     BUFSIZ1,    BUFSIZ2,    BUFSIZ32
  298. };
  299. #ifdef    GETSPEED
  300. /* Baud rates 75 and 50 are not currently decoded. */
  301. int    timevals[] = {
  302.     1,    2,    4,    5,    8,    10,    11,
  303.     16,    32,    64,    96,    128,    143,    175
  304. };
  305. #endif
  306.  
  307. int    speed = -1;        /* Don't know */
  308. int    flow = -1;
  309. int    ucr = -1;
  310. struct iorec old_iorec;
  311.  
  312. /* load rs232 configuration into RSCONF dialog */
  313. getrsconf ()
  314. {
  315.     OBJECT    *obj;
  316.     register int    i;
  317. #ifdef    GETSPEED
  318. #define    MFP    0xFFFFFA01L
  319. #define    TDDR    36
  320. #define    hz_200    ((unsigned long *) 0x4BAL)
  321.     int    tv;            /* Timer value */
  322.     int    maxtv;            /* Maximum timer value */
  323.     unsigned long    endhz;        /* End of polling period */
  324.     long    savessp;        /* Old stack pointer */
  325. #endif
  326. #ifdef    GETFLOW
  327.     struct    rsiorec    {
  328.         char    fill0[32];
  329.         char    rsmode;
  330.         char    fill1[1];
  331.     };
  332. #endif
  333.  
  334. #ifdef    GETSPEED
  335.     savessp = Super(0L);
  336.     maxtv = 0;
  337.     endhz = *hz_200 + 8;
  338.     while (*hz_200 < endhz)
  339.     {
  340.         tv = *((unsigned char *) (0xFFFFFA01L + 36));
  341.         if (tv > maxtv)
  342.             maxtv = tv;
  343.     }
  344.     (void) Super(savessp);
  345.     for (i = (int)(sizeof(timevals) / sizeof(timevals[0]));
  346.       --i >= 0 && timevals[i] != maxtv; );
  347.     if (i >= 0)
  348.         speed = i;
  349. #endif
  350. #ifdef    GETFLOW
  351.     flow = ((struct rsiorec *) Iorec(0))->rsmode;
  352.     if (flow == 3)            /* UW doesn't support both */
  353.         flow = 1;
  354. #endif
  355. #ifdef    GETUCR
  356.     /* Dev. Kit code suggests that Rsconf() returns old register vals */
  357.     ucr = (Rsconf( -1, -1, -1, -1, -1, -1) >> 24) & 0xFF;
  358. #endif
  359.     rsrc_gaddr(R_TREE, RSCONF, &obj);
  360.     for (i = (int)(sizeof(speedobjs) / sizeof(int)); --i >= 0; )
  361.         obj[speedobjs[i]].ob_state = NORMAL;
  362.     for (i = (int)(sizeof(flowobjs) / sizeof(int)); --i >= 0; )
  363.         obj[flowobjs[i]].ob_state = NORMAL;
  364.     for (i = (int)(sizeof(parobjs) / sizeof(int)); --i >= 0; )
  365.         obj[parobjs[i]].ob_state = NORMAL;
  366.     for (i = (int)(sizeof(stopobjs) / sizeof(int)); --i >= 0; )
  367.         obj[stopobjs[i]].ob_state = NORMAL;
  368.     for (i = (int)(sizeof(bcbobjs) / sizeof(int)); --i >= 0; )
  369.         obj[bcbobjs[i]].ob_state = NORMAL;
  370.     for (i = (int)(sizeof(bufobjs) / sizeof(int)); --i >= 0; )
  371.         obj[bufobjs[i]].ob_state = NORMAL;
  372.  
  373.     if (speed >= 0)
  374.         obj[speedobjs[speed]].ob_state = SELECTED;
  375.  
  376.     if (flow >= 0)
  377.         obj[flowobjs[flow]].ob_state = SELECTED;
  378.  
  379.     if (ucr >= 0)
  380.     {
  381.         switch (ucr & 0x6)    /* Parity */
  382.         {
  383.         default:
  384.             obj[PBNONE].ob_state = SELECTED;
  385.             break;
  386.         case 0x4:
  387.             obj[PBODD].ob_state = SELECTED;
  388.             break;
  389.         case 0x6:
  390.             obj[PBEVEN].ob_state = SELECTED;
  391.             break;
  392.         }
  393.     
  394.         switch (ucr & 0x18)
  395.         {
  396.         default:
  397.             obj[SBB1].ob_state = SELECTED;
  398.             break;
  399.         case 0x10:
  400.             obj[SBB15].ob_state = SELECTED;
  401.             break;
  402.         case 0x18:
  403.             obj[SBB2].ob_state = SELECTED;
  404.             break;
  405.         }
  406.     
  407.         obj[bcbobjs[(ucr >> 5) & 3]].ob_state = SELECTED;
  408.     }
  409.     obj[bufobjs[bufsiz]].ob_state = SELECTED;
  410. }
  411.     
  412. /* set rs232 configuration from the RSCONF dialog */
  413. setrsconf ()
  414. {
  415.     register int    i;
  416.     OBJECT    *obj;
  417.     short    parity = 0;
  418.     short    stop = 0;
  419.     short    bcb = -1;
  420.  
  421.     rsrc_gaddr(R_TREE, RSCONF, &obj);
  422.  
  423.     for (i = (int)(sizeof(speedobjs) / sizeof(int)); --i >= 0; )
  424.         if (obj[speedobjs[i]].ob_state & SELECTED)
  425.         {
  426.             speed = i;
  427.             break;
  428.         }
  429.  
  430.     for (i = (int)(sizeof(flowobjs) / sizeof(int)); --i >= 0; )
  431.         if (obj[flowobjs[i]].ob_state & SELECTED)
  432.         {
  433.             flow = i;
  434.             break;
  435.         }
  436.  
  437.     for (i = (int)(sizeof(parobjs) / sizeof(int)); --i >= 0; )
  438.         if (obj[parobjs[i]].ob_state & SELECTED)
  439.         {
  440.             parity = i + 1;
  441.             break;
  442.         }
  443.  
  444.     for (i = (int)(sizeof(stopobjs) / sizeof(int)); --i >= 0; )
  445.         if (obj[stopobjs[i]].ob_state & SELECTED)
  446.         {
  447.             stop = i + 1;
  448.             break;
  449.         }
  450.  
  451.     for (i = (int)(sizeof(bcbobjs) / sizeof(int)); --i >= 0; )
  452.         if (obj[bcbobjs[i]].ob_state & SELECTED)
  453.         {
  454.             bcb = i;
  455.             break;
  456.         }
  457.  
  458.     for (i = (int)(sizeof(bufobjs) / sizeof(int)); --i >= 0; )
  459.         if (obj[bufobjs[i]].ob_state & SELECTED)
  460.         {
  461.             bufsiz = i;
  462.             break;
  463.         }
  464.  
  465.     if ((parity || stop || bcb >= 0) && ucr < 0)
  466.         ucr = DEFUCR;
  467.     if (parity)
  468.         ucr = (ucr & ~0x6) | (parity << 1);
  469.     if (stop)
  470.         ucr = (ucr & ~0x18) | (stop << 3);
  471.     if (bcb >= 0)
  472.         ucr = (ucr & ~0x60) | (bcb << 5);
  473.  
  474.     Rsconf(speed, flow, ucr, -1, -1, -1);
  475.     rsbufset(bufsiz);
  476. }
  477.  
  478. void rsbufset(bufsiz)
  479. int bufsiz;
  480. {
  481.     struct iorec *ioptr;
  482.     static char    *memalloced = NULL;
  483.     static int    oldbufsiz = 0;
  484.  
  485.     ioptr = Iorec(0);
  486.     if (old_iorec.io_bufsiz == 0)
  487.         old_iorec = *ioptr;    /* save gem buffer */
  488.     if (oldbufsiz != bufsiz) {
  489.         char *mem;
  490.         oldbufsiz = bufsiz;
  491.         mem = (char *) Malloc(bufsizs[bufsiz]);
  492.         if (mem != NULL) {
  493.             if (memalloced != NULL)
  494.                 Mfree(memalloced);
  495.             memalloced = mem;
  496.             ioptr->io_buff = mem;
  497.             ioptr->io_bufsiz = bufsizs[bufsiz];
  498.             ioptr->io_head = 0;
  499.             ioptr->io_tail = 0;
  500.             ioptr->io_low = bufsizs[bufsiz] / 4;
  501.             ioptr->io_high = ioptr->io_bufsiz - ioptr->io_low;
  502.         }
  503.     }
  504. }
  505.