home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / doc / tutorials / resources.txt < prev    next >
Encoding:
Text File  |  1988-10-26  |  12.7 KB  |  305 lines

  1.  
  2.                Using and Specifying X Resources
  3.  
  4.                   Jim Fulton
  5.                                MIT X Consortium
  6.  
  7.          Copyright 1988 Massachusetts Institute of Technology
  8.  
  9. Permission to  use, copy, modify,   and distribute this  documentation  for any
  10. purpose and without  fee  is hereby granted,  provided that the above copyright
  11. notice appear in  all copies and   that  both that  copyright  notice  and this
  12. permission  notice  appear in  supporting documentation,  and  that the name of
  13. M.I.T.  not be used in advertising  or publicity pertaining to  distribution of
  14. the software without specific, written prior permission.
  15.  
  16.                     * * * *
  17.  
  18.  
  19. The Xlib Resource Manager provides a set of tools for specifying and
  20. manipulating user preferences (e.g. geometry, colors, fonts).  Simple
  21. programming interfaces to it are provided by the X Toolkit and by the Xlib
  22. routine XGetDefault.
  23.  
  24. Resources, also refered to as "defaults" in older versions of X, are simply
  25. <name,value> pairs that are frequently used to control the appearence or
  26. function of particular program or subsystem.  They provide a convenient way to
  27. tailor whole collections of applications with a minimal amount of work.
  28.  
  29. In previous versions of X, defaults were stored in a .Xdefaults file in each
  30. user's home directory, on every machine.  In addition to requiring duplicate
  31. copies of the defaults, it did not lend itself to conditional specifications
  32. (particularly if the user used both monochrome and color displays).
  33.  
  34. In X11, these problems are solved by using the window property mechanism in the
  35. X protocol to store resources in the server, where they are available to all
  36. clients.  As a result, defaults may now be dynamically specified based on the
  37. particular display being used.  This is particularly useful in setting up
  38. different defaults for monochrome and color displays.  Furthermore, a new
  39. convention for specifying resources from the command line has been established
  40. (and is supported by all clients of the X Toolkit).
  41.  
  42. Resources are usually as "program.name.subname.etc: value", one per line in
  43. resource files, or one per -xrm argument.  Names are hierarchies, usually
  44. corresponding to major structures within an application (where structures are
  45. often objects like windows, panels, menus, scrollbars, etc.).  The various
  46. subnames are called components and are specified left to right from most
  47. general to least general.
  48.  
  49. If we take the "xmh" application as an example, we can see that its display
  50. is made up of sections called "panes", some of which in turn contain
  51. command buttons.  The "include" button (used to retreive new mail) in the
  52. "toc" (table of contents) pane could be named as follows:
  53.  
  54.     program        pane        object group    subobject
  55.     name        name        name        name
  56.  
  57.     xmh        toc        buttons        include
  58.  
  59. An object's fully specified name (such as "xmh.toc.buttons.include" in the 
  60. example above) is called the "instance" name for that object.  In addition,
  61. each component belongs to a collection of similar components (such as the set
  62. of all panes, the set of all buttons, etc.) that can be specified using a 
  63. "class" name.  In the above example, if we assume that the xmh program is
  64. one of possibly several "Xmh" types of programs (just as "gnuemacs" and
  65. "microemacs" might be thought of as instances of the class of "Emacs"
  66. programs), we could build the following class name:
  67.  
  68.     application    top level    second level    third level
  69.     type        area type    object type    object type
  70.  
  71.     Xmh        VPaned        Box        Command        ...
  72.  
  73. By convention, instance name components begin with lowercase letters and class
  74. name components begin with uppercase letters.  Components that are made up of
  75. more than one word have the succeeding words capitalized and concatentated
  76. without any spaces.  Thus, an instance of an icon pixmap might be called
  77. "iconPixmap" whereas the class of icon pixmaps would be called "IconPixmap".
  78. The capitalization is important because resource names may contain both
  79. instance and class name components within the same specification
  80. (for example, "gnuemacs.VPaned.cursorColor: blue").
  81.  
  82. Class names allow default values to be specified for all versions of given
  83. object.  Instance names allow a value for a particular version to be given
  84. that overrides the class value, and can be used to specify exceptions to the
  85. rules outlined by the class names.  For example, we could specify that
  86. that all command buttons in button boxes in vertical panes should have a 
  87. background color of blue, except for "include", which should be red: with
  88.  
  89.     *VPaned.Box.Command.Background:  blue
  90.     xmh.toc.buttons.include.background:  red
  91.  
  92. Furthermore, resource name hierarchies do not have to be fully specified.  In
  93. the preceeding example, we listed each of the individual components; however,
  94. this can be quite cumbersome.  Instead of having to give a full specification
  95. for each set of objects (there might be slider bars, or edit windows, or any
  96. number of other types), we can just "wildcard", or omit, the intervening
  97. components by using the "*" separator in place of the "." separator.  In
  98. general, it is a good idea to use the "*" instead of "." in case you've
  99. forgotten any intervening components or in case new levels are inserted into
  100. the middle of the hierarchy.
  101.  
  102.     Xmh*VPaned*Background:  blue
  103.     xmh*toc.buttons.include.background: red
  104.  
  105. The distiction between classes and instances is important.  For example,
  106. many text applications have some notion of background, foreground, border, 
  107. pointer, and cursor or marker color.  Usually the background is set to one
  108. color, and all of the other attributes are set to another so that they may be
  109. seen on a monochrome display.  To allow users of color displays to set any or
  110. all of them, the colors may be organized into classes as follows:
  111.  
  112.     instance name        class name
  113.  
  114.     background        Background
  115.     foreground        Foreground
  116.     borderColor        Foreground
  117.     pointerColor        Foreground
  118.     cursorColor        Foreground
  119.  
  120. Then, to configure the application to run in "monochrome" mode, but using
  121. two colors, you would only have to use two specifications:
  122.  
  123.     obj*Background:  blue
  124.     obj*Foreground:  red
  125.  
  126. Then, if you decided that you wanted the cursor to be yellow, but the pointer
  127. and the border to remain the same as the foreground, you would only need one
  128. new resource specification:
  129.  
  130.     obj*cursorColor: yellow
  131.  
  132. Because class and instance names are distinguishable by case, both types of
  133. names may be given in a single resource specification.  Section 10.11 of the
  134. Xlib manual gives the following additional examples (note, the "xmh" program
  135. may use other names, these are for example only):
  136.  
  137.     xmh*background:                      red
  138.     *command.font:                       8x13
  139.     *command.background:                 blue
  140.     *Command.Foreground:                 green
  141.     xmh*toc*Command.activeForeground:    black
  142.  
  143. The resource hierarchy "xmh.toc*Command.activeForeground" specifies a
  144. particular color resource (in this case, the active foreground color) of all
  145. components of class Command that are contained within the "toc" in the xmh
  146. application.  Although this is very powerful, figuring out that this can be
  147. specified at all, let alone how, is currently a problem with the documentation
  148. for many of the more complex clients of the Resource Manager.  Eventually,
  149. widgets should be documented just as commands are today: there should be
  150. descriptions of the instance names, class names, and allowable values for each
  151. of the widget's resources.  Application documentation would then only need to
  152. describe how widgets are combined.  Until then, the best places to look for
  153. information on which resources may be specified are:
  154.  
  155.     1. the manual pages for the application
  156.     2. any documentation for the widgets used by the application
  157.     3. any application resource files in /usr/lib/X11/app-defaults/
  158.     4. any XtResource tables in the the application or the widget sources
  159.  
  160. Under X11, you have a lot of flexibility as to where defaults are defined.  The
  161. Resource Manager obtains resource specifications from the following places:
  162.  
  163.     1. from any application-specific resource files, usually stored in
  164.        /usr/lib/X11/app-defaults/.
  165.  
  166.     2. from any application-specific resources files in the directory
  167.        named by the environment variable XAPPLRESDIR (default value
  168.        is $HOME) for programs written using the X Toolkit.
  169.  
  170.     3. from the RESOURCE_MANAGER property on the root window of screen 0;
  171.        these are stored using the xrdb program.  If this property is not
  172.        defined, then $HOME/.Xdefaults will be read to provide compatibility
  173.        with X10 (although the resource specification format has
  174.        changed somewhat).
  175.  
  176.     4. from any user-specific defaults stored in a file whose name is set
  177.        in the environment variable XENVIRONMENT.
  178.  
  179.     5. from the -xrm command line option (for programs written with the
  180.        X Toolkit).
  181.  
  182.  
  183. Resources are usually loaded from a file into the RESOURCE_MANAGER property
  184. using the "xrdb" program from whatever script you use to start up X.  Sites
  185. that use the xdm Display Manager will most likely provide for loading in
  186. user-specified resources automatically.  See your system manager for details.
  187.  
  188. I have a script called "xsetup" on every machine that I use that starts up the
  189. appropriate programs for that machine (terminal emulators, a mailbox on my home
  190. machine, clocks, load average monitors, etc.).  For example, I use the
  191. following on my desktop workstation to get my base environment going:
  192.  
  193.     #!/bin/sh
  194.     xrdb -load $HOME/.Xresources
  195.     stty erase '^?'
  196.     xmodmap -p $HOME/.xmodmap
  197.     xset b 100 400 c 50 s 1800
  198.     uwm &
  199.     xclock -geometry 48x48-1+1 &
  200.     xload -geometry 48x48-1+100 &
  201.  
  202. The xrdb program reads my global defaults from $HOME/.Xresources.  This is
  203. where I define resources that I want to have used by clients on every machine:
  204.  
  205.     bitmap*Dashed:  off
  206.     XTerm*multiScroll:  on
  207.     XTerm*jumpScroll:  on
  208.     XTerm*reverseWrap:  on
  209.     XTerm*curses:  on
  210.     XTerm*font:  6x10
  211.     emacs*Geometry:  80x65-0-0
  212.     emacs*Background:  #5b7686
  213.     emacs*Foreground:  white
  214.     emacs*Cursor:  white
  215.     emacs*BorderColor:  white
  216.     emacs*Font:  6x10
  217.  
  218. I put machine-specific defaults (usually colors so that I can easily
  219. distinguish windows on various machines) in a file called ~/.Xenv and
  220. set the XENVIRONMENT variable in my .login to point to that file.  For 
  221. example, I use the following defaults on my desktop workstation:
  222.  
  223.     XTerm*Background:  black
  224.     XTerm*Foreground:  green
  225.     XTerm*BorderColor:  white
  226.     xclock*analog:  on
  227.     xclock*borderWidth: 0
  228.     xclock*padding: 2
  229.     xclock*Background: black
  230.     xclock*Foreground: red
  231.     xload*Background: black
  232.     xload*Foreground: cyan
  233.  
  234. For consistency, I create an xsetup script and a .Xenv file (if I want any
  235. machine specific defaults) on each of the machines that I commonly use.  This
  236. allows me to just login, type "xsetup", and get right to work.  Continuing
  237. the example started above, here is the xsetup file that I use on my server:
  238.  
  239.     #!/bin/sh
  240.     xterm -geometry 80x55+0+0 &
  241.     xterm -geometry 80x65+488+1 &
  242.     xterm -geometry 80x20+0-0  &
  243.     xload =48x48-1+150 &
  244.     xbiff -rv =48x48-1+50 &
  245.  
  246. and the corresponding .Xenv file:
  247.  
  248.     XTerm*Foreground:  white
  249.     XTerm*Background:  #c00
  250.     XTerm*BorderColor:  white
  251.     tinyxterm*Font: 3x5
  252.     tinyxterm*Geometry: 80x24
  253.     bigxterm*Font: 9x15
  254.     bigxterm*Geometry:  80x55
  255.     xload*Background: black
  256.     xload*Foreground: red
  257.     xbiff*borderWidth: 0
  258.     xbiff*Background: white
  259.     xbiff*Foreground: blue
  260.     xbiff*reverseVideo: on
  261.  
  262. Note the use of the alternative instance names "tinyxterm" and "bigxterm".
  263. This allows me to quickly get a different "flavor" of xterm by simply 
  264. invoking "xterm -name tinyxterm" or "xterm -name bigxterm".
  265.  
  266. The xrdb program uses the C preprocessor to provide conditionals based on
  267. the configuration of the server being used.  This is very useful if you 
  268. commonly use different types of servers (such as monochrome and color) 
  269. from the same account.  See the xrdb(1) manual page for more details.
  270.  
  271.  
  272.  
  273. Summary
  274.  
  275. X11 gives you many choices as to how to organize your screen, specify your
  276. defaults, and start up various programs.  I frequently have 5 xterms, 1 clock,
  277. 2 load average monitors, 1 mailbox, an emacs, and uwm all running at once.
  278. The scheme outlined above is somewhat of a brute force approach, but works
  279. well for me.
  280.  
  281. The highlights are:
  282.  
  283.     1.  Use the Display Manager xdm.  It is very flexible and can be 
  284.         tailored to provide any sort of default environment.
  285.  
  286.     2.  Create X startup scripts that run the appropriate programs to set
  287.         up your environment (start your window manager, load defaults, 
  288.         etc.).  I call these scripts "xsetup" on each machine.
  289.  
  290.     3.  Put global resources in one file that you run xrdb on from your
  291.         X startup script.  I call this file ~/.Xresources.  In general, 
  292.         you should use "*" instead of "." to separate components.
  293.  
  294.     4.  Put machine-specific resources in another file and set the 
  295.         XENVINRONMENT variable to point to it.  I call this file ~/.Xenv.
  296.         Use "*" instead of "." to separate components.
  297.  
  298.  
  299.  
  300. Where to look for more information:
  301.  
  302.     X(1), xrdb(1), Section 10.11 of the Xlib manual, XGetDefault(3X), 
  303.     Section 4.2 of the X Toolkit manual, application manual pages
  304.  
  305.