home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / may94 / util / edit / jade.lha / Jade / src / x11_display.c < prev    next >
C/C++ Source or Header  |  1994-04-19  |  6KB  |  230 lines

  1. /* x11_display.c -- Initialisation for X11 window-system
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4. This file is part of Jade.
  5.  
  6. Jade is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Jade is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Jade; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "jade.h"
  21. #include "jade_protos.h"
  22.  
  23. #include <X11/Xutil.h>
  24.  
  25. #ifdef HAVE_UNIX
  26. # include <sys/types.h>
  27. # include <sys/time.h>
  28. # include <fcntl.h>
  29. #else
  30.   you lose
  31. #endif
  32.  
  33. _PR void sys_usage(void);
  34. _PR int initwinsys(int *, char ***);
  35. _PR void killwinsys(void);
  36.  
  37. #ifdef HAVE_UNIX
  38. _PR fd_set FdReadSet;
  39. fd_set FdReadSet;
  40. _PR void (*FdReadAction[])(int);
  41. void (*FdReadAction[FD_SETSIZE])(int);
  42. #endif
  43.  
  44. _PR Display *XDisplay;
  45. _PR int XScreen;
  46. _PR Colormap ColourMap;
  47. _PR u_long XForePixel, XBackPixel;
  48. _PR Atom Wm_Del_Win;
  49. _PR char **Argv;
  50. _PR int Argc;
  51.  
  52. Display *XDisplay;
  53. int XScreen;
  54. Colormap XColourMap;
  55. u_long XForePixel, XBackPixel;
  56. Atom Wm_Del_Win;
  57. char **Argv;
  58. int Argc;
  59.  
  60. static char *DisplayName = NULL;
  61. static char *BgStr = "white";
  62. static char *FgStr = "black";
  63. static char *GeomStr = "80x24";
  64. _PR VALUE DefFontStr;
  65. VALUE DefFontStr = MKSTR(DEFAULT_FONT);
  66. static char *ProgName;
  67.  
  68. static int
  69. getxresources(void)
  70. {
  71.     char *s;
  72.     if((s = XGetDefault(XDisplay, ProgName, "geometry"))
  73.        || (s = XGetDefault(XDisplay, "Jade", "geometry")))
  74.     GeomStr = s;
  75.     if((s = XGetDefault(XDisplay, ProgName, "foreground"))
  76.        || (s = XGetDefault(XDisplay, "Jade", "foreground")))
  77.     FgStr = s;
  78.     if((s = XGetDefault(XDisplay, ProgName, "background"))
  79.        || (s = XGetDefault(XDisplay, "Jade", "background")))
  80.     BgStr = s;
  81.     if((s = XGetDefault(XDisplay, ProgName, "font"))
  82.        || (s = XGetDefault(XDisplay, "Jade", "font")))
  83.     DefFontStr = valstrdup(s);
  84.     return(TRUE);
  85. }
  86. void
  87. sys_usage(void)
  88. {
  89.     fputs(
  90.     "where SYSTEM-OPTIONS are,\n"
  91.     "    -display DISPLAY-NAME\n"
  92.     "    -name NAME\n"
  93.     "    -geometry WINDOW-GEOMETRY\n"
  94.     "    -fg FOREGROUND-COLOUR\n"
  95.     "    -bg BACKGROUND-COLOUR\n"
  96.     "    -font FONT-NAME\n"
  97.     "    -sync\n"
  98.     , stderr);
  99. }
  100. static int
  101. getxoptions(int *argc_p, char ***argv_p)
  102. {
  103.     int argc = *argc_p;
  104.     char **argv = *argv_p;
  105.     argc--;
  106.     argv++;
  107.     while((argc >= 1) && (**argv == '-'))
  108.     {
  109.     if(!strcmp("-sync", *argv))
  110.         XSynchronize(XDisplay, True);
  111.     else if(argc >= 2)
  112.     {
  113.         if(!strcmp("-display", *argv))
  114.         ;
  115.         else if(!strcmp("-name", *argv))
  116.         ;
  117.         else if(!strcmp("-geometry", *argv))
  118.         GeomStr = argv[1];
  119.         else if(!strcmp("-fg", *argv))
  120.         FgStr = argv[1];
  121.         else if(!strcmp("-bg", *argv))
  122.         BgStr = argv[1];
  123.         else if(!strcmp("-font", *argv))
  124.         DefFontStr = valstrdup(argv[1]);
  125.         else
  126.         break;
  127.         argc--; argv++;
  128.     }
  129.     else
  130.         break;
  131.     argc--; argv++;
  132.     }
  133.     *argc_p = argc;
  134.     *argv_p = argv;
  135.     return(TRUE);
  136. }
  137. static int
  138. usexoptions(void)
  139. {
  140.     int x, y, w, h;
  141.     int gflgs = XParseGeometry(GeomStr, &x, &y, &w, &h);
  142.     XColor tmpc;
  143.     if(gflgs & WidthValue)
  144.     DefDims[2] = w;
  145.     else
  146.     DefDims[2] = -1;
  147.     if(gflgs & HeightValue)
  148.     DefDims[3] = h;
  149.     else
  150.     DefDims[3] = -1;
  151.     /*
  152.      * need to use -ve values properly
  153.      */
  154.     if(gflgs & XValue)
  155.     DefDims[0] = x;
  156.     else
  157.     DefDims[0] = -1;
  158.     if(gflgs & YValue)
  159.     DefDims[1] = y;
  160.     else
  161.     DefDims[1] = -1;
  162.     if(!XParseColor(XDisplay, XColourMap, FgStr, &tmpc))
  163.     fprintf(stderr, "error: invalid fg colour\n");
  164.     else if(!XAllocColor(XDisplay, XColourMap, &tmpc))
  165.     fprintf(stderr, "error: can't allocate fg colour\n");
  166.     else
  167.     XForePixel = tmpc.pixel;
  168.     if(!XParseColor(XDisplay, XColourMap, BgStr, &tmpc))
  169.     fprintf(stderr, "error: invalid bg colour\n");
  170.     else if(!XAllocColor(XDisplay, XColourMap, &tmpc))
  171.     fprintf(stderr, "error: can't allocate bg colour\n");
  172.     else
  173.     XBackPixel = tmpc.pixel;
  174.     return(TRUE);
  175. }
  176.  
  177. int
  178. initwinsys(int *argc_p, char ***argv_p)
  179. {
  180.     int i;
  181.     Argv = *argv_p;
  182.     Argc = *argc_p;
  183.     ProgName = filepart(Argv[0]);
  184.     for(i = 1; i < Argc; i++)
  185.     {
  186.     /* These options have to be found *before* resources are looked
  187.        for.     */
  188.     if(!strcmp("-display", Argv[i]) && (i+1 < Argc))
  189.         DisplayName = Argv[++i];
  190.     else if(!strcmp("-name", Argv[i]) && (i+1 < Argc))
  191.         ProgName = Argv[++i];
  192.     }
  193.     XDisplay = XOpenDisplay(DisplayName);
  194.     if(XDisplay)
  195.     {
  196. #ifdef HAVE_UNIX
  197.     FD_ZERO(&FdReadSet);
  198.     /*
  199.      * close-on-exec = TRUE
  200.      */
  201.     fcntl(ConnectionNumber(XDisplay), F_SETFD, 1);
  202.     FD_SET(ConnectionNumber(XDisplay), &FdReadSet);
  203. /*
  204.  * Don't need to do this since the fact that the select() has returned
  205.  * means that the input Q is re-checked...
  206.  * ...although having thought about this it may be useful to do the
  207.  * below stuff so all events are processed before reading output from
  208.  * processes???
  209.  *    FdReadAction[ConnectionNumber(XDisplay)] = readnextevent;
  210.  */
  211. #endif /* HAVE_UNIX */
  212.  
  213.     XScreen = DefaultScreen(XDisplay);
  214.     XColourMap = DefaultColormap(XDisplay, XScreen);
  215.     if(getxresources() && getxoptions(argc_p, argv_p) && usexoptions())
  216.     {
  217.         Wm_Del_Win = XInternAtom(XDisplay, "WM_DELETE_WINDOW", False);
  218.         return(TRUE);
  219.     }
  220.     }
  221.     else
  222.     fprintf(stderr, "jade: can't open X connection\n");
  223.     return(FALSE);
  224. }
  225. void
  226. killwinsys(void)
  227. {
  228.     XCloseDisplay(XDisplay);
  229. }
  230.