home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / doc / tutorials / bugtalk.txt < prev    next >
Encoding:
Text File  |  1989-10-10  |  19.7 KB  |  516 lines

  1. (Message inbox:31)
  2. Return-Path: dshr@sun.com
  3. Received: by expo.lcs.mit.edu; Mon, 6 Feb 89 12:47:48 EST
  4. Received: by ATHENA.MIT.EDU (5.45/4.7) id AA21584; Mon, 6 Feb 89 12:47:38 EST
  5. Received: from snail.Sun.COM by Sun.COM (4.1/SMI-4.0)
  6.     id AA11194; Mon, 6 Feb 89 09:49:55 PST
  7. Received: from devnull.sun.com by snail.Sun.COM (4.1/SMI-4.0)
  8.     id AA13577; Mon, 6 Feb 89 09:46:40 PST
  9. Received: by devnull.sun.com (4.0/SMI-4.0)
  10.     id AA14389; Mon, 6 Feb 89 09:44:41 PST
  11. Message-Id: <8902061744.AA14389@devnull.sun.com>
  12. Date: Mon 6 Feb 1989 09:36:43 EST
  13. From: David Rosenthal <dshr@sun.com>
  14. Subject: Usenix talk & paper
  15. To: xpert@athena.mit.edu
  16.  
  17. Last week,  I presented a paper at Usenix that David Lemke and I had
  18. written on the problem of choosing and using Visuals.  Several people
  19. asked me to post the talk,  and it is attached below.  Also,  we found
  20. a bug in the paper printed in the Proceedings,  and I will be posting
  21. a shar file with the corrected source for the paper.  The bug was that
  22. the code in the Proceedings chooses the largest available Visual,  and
  23. you should choose the SMALLEST avaialable Visual that will do the job,
  24. so as not to waste resources.
  25.  
  26.     David.
  27.  
  28.  
  29.  
  30.  
  31.  
  32.                 San Diego Usenix Talk Script
  33.  
  34.  
  35.                    David S. H. Rosenthal
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. Introduction
  43.  
  44. Good morning.  I'm  Sgt.  Rosenthal  of  the  Softies,   the
  45. Software  Police.   Even  though  its  early,   you'd better
  46. listen up and listen good, because what I'm  going  to  tell
  47. you  this morning can keep some of you out of a lot of trou-
  48. ble.
  49.  
  50. Officer Lemke and I work in the X Division, and  we've  been
  51. out  there on the net making undercover buys of X games pro-
  52. grams.  When we test them back in the labs, we almost always
  53. find recreational bugs.
  54.  
  55. The Zero-Tolerance Bugs Policy
  56.  
  57. I'm  here  today  to  talk   to   you   about   the   Quayle
  58. administration's  new  ``zero-tolerance''  policy  on  bugs.
  59. Nancy Reagan's "Just Say No" to bugs program  was  all  very
  60. well,  but it didn't stop the wave of bugs that engulfed the
  61. nation  during  her  husband's  administration.    The   new
  62. administration has decided to get tough on bugs,  and that's
  63. what the zero-tolerance policy is all about.
  64.  
  65. Surveys tell us that many of you have pretty tolerant  atti-
  66. tudes to recreational bugs.  I'm here to change that.  There
  67. are three main reasons why the policy  focusses  on  recrea-
  68. tional bugs:
  69.  
  70. +  In most of the cases we see,   programs with recreational
  71.    bugs go on to develop more serious hard bugs.
  72.  
  73. +  The sharing of  software  involved  in  recreational  bug
  74.    usage can lead to the transmission of viruses,  worms and
  75.    other parasites.
  76.  
  77. +  And,  of course,  the bottom  line  is  that  bug-free  X
  78.    software isn't just a good idea,  its the law.
  79.  
  80. Penalties for Bug Offences
  81.  
  82. Under the zero-tolerance policy the  penalties  for  even  a
  83. small recreational bug can include:
  84.  
  85. +  confiscation of resources,
  86.  
  87. +  X protocol errors,
  88.  
  89. +  termination of your program,
  90.  
  91. +  visions of bizarre colors on your screen,
  92.  
  93. +  and above all making the designers of X unhappy.
  94.  
  95. You do not want to make the X designers  unhappy.   If  they
  96. get  too  unhappy  with the way you're mis-using their crea-
  97. tion, they may just get together and build you a  whole  new
  98. industry-standard  window system.  Look how much work it was
  99. last time they did this.  Think what  they've  learnt  since
  100. then.
  101.  
  102. The Neighbourhood Bug-Watch Program
  103.  
  104. Everyone deserves a bug-free environment, but  achieving  it
  105. isn't  easy.  The scourge of bugs has affected every stratum
  106. of society.  People you trust implicitly,  role-models  like
  107. the  folks at MIT and elsewhere who work long hours into the
  108. night selflessly donating their software to the  poor,  even
  109. these people may be into recreational bugs.  And you may not
  110. find out until its too late.  When we in  the  Softies  stop
  111. you  out  there  on the net, it won't do you any good to say
  112. ``This code isn't mine,  I'm just holding it for a friend''.
  113.  
  114. Types of Bug to Look Out For
  115.  
  116. X bugs you should be looking for can be divided into:
  117.  
  118. +  The narcotics that leave programs catatonic.   A  warning
  119.    sign to look for is a program that starts ignoring Expose
  120.    events.  You've heard plaintive complaints from people on
  121.    xpert  that  they  draw  and draw and nothing is visible;
  122.    they are suffering from narcotics abuse.
  123.  
  124. +  The stimulants that drive  programs  into  a  frenzy.   A
  125.    warning  sign  is  a  program  that  tries  to argue with
  126.    authority figures like window managers.
  127.  
  128. +  The steroids that cause programs (and especially servers)
  129.    to swell up and get huge.
  130.  
  131. +  The hallucinogens that cause swirling visions in  bizarre
  132.    colors.
  133.  
  134. This morning I'm going to review the hallucinogens and their
  135. symptoms, and we need to start with some basic physiology.
  136.  
  137. Basic X Physiology
  138.  
  139. When your X client draws,  it does so in a Drawable, a  rec-
  140. tangle  of  memory locations containing numbers called pixel
  141. values.  The drawing  operations  like  CopyArea,  PolyLine,
  142. PutImage and PolyText8 all change these pixel values.
  143.  
  144. Some of these drawables may be windows, and some of the win-
  145. dows may be mapped.  Every few milliseconds,  the screen you
  146. are looking at is refreshed by reading  the  pixels  of  the
  147. visible  parts  of  the  the mapped windows, and using these
  148. values to index into a colormap.  The colormap cell  indexed
  149. by the pixel value contains a red,  a green and a blue value
  150. that is fed to the corresponding gun of the screen.
  151.  
  152. The details of this process  can  differ  between  different
  153. types  of hardware.  The differences are exposed; X gives no
  154. guarantee of portability and you have to be aware  of  these
  155. differences:
  156.  
  157. +  Is there one gun (monochrome) or three (color)?
  158.  
  159. +  How many entries in a colormap?
  160.  
  161. +  Is there one colormap,  or one colormap per gun?
  162.  
  163. +  How many colormaps are simultaneously accessible?
  164.  
  165. +  Can a client write the colormap entries?
  166.  
  167. +  How many bits in each colormap entry for each gun?
  168.  
  169. All these differences are collected together into a  concept
  170. called  a  Visual, and the possible Visuals divided into six
  171. classes:
  172.  
  173. +  StaticGray.   The  pixel  value  indexes  a   predefined,
  174.    read-only  colormap.   For  each colormap cell,  the red,
  175.    green and blue values are the  same,   producing  a  gray
  176.    image.
  177.  
  178. +  StaticColor.  The  pixel  values  indexes  a  predefined,
  179.    read-only  colormap.  The red,  green and blue values for
  180.    each cell are server-dependent.
  181.  
  182. +  TrueColor.  The pixel value is  divided  into  sub-fields
  183.    for  red,   green  and  blue.   Each sub-field separately
  184.    indexes the appropriate primary of a  predefined,   read-
  185.    only  colormap.  The red,  green and blue values for each
  186.    cell are server-dependent, and are selected to provide  a
  187.    nearly linear increasing ramp.
  188.  
  189. +  GrayScale.  The pixel value indexes a colormap which  the
  190.    client  can  alter,   subject to the restriction that the
  191.    red,  green and blue values of each cell must  always  be
  192.    the same,  producing a gray image.
  193.  
  194. +  PseudoColor.  The pixel value indexes  a  colormap  which
  195.    the client can alter.  The red,  green and blue values of
  196.    each cell can be selected arbitrarily.
  197.  
  198. +  DirectColor.  The pixel value is divided into  sub-fields
  199.    for  red,   green  and  blue.   Each sub-field separately
  200.    indexes the appropriate primary of a  colormap  that  the
  201.    client can alter.
  202.  
  203. When you implement an X server for an individual workstation
  204. configuration,  you have to decide which Visuals you want to
  205. make available to your clients.  Sometimes,  the  choice  is
  206. obvious.  For example,  a normal monochrome workstation like
  207. a Sun 3/50 would naturally export  a  single  depth  1  Sta-
  208. ticGray  Visual.   A  simple  color  workstation  like a Sun
  209. 3/160C with an 8-bit-per-pixel frame  buffer  and  a  single
  210. 256-entry  colormap  would naturally export a single depth 8
  211. PseudoColor Visual.
  212.  
  213. As hardware gets more complex,  the choice may be to  export
  214. several  Visuals for a single screen.  For example,  the Sun
  215. 3/110C in effect has a 10-bit deep frame buffer.  8  of  the
  216. bits  index  a  single  256-entry colormap,  one of the bits
  217. acts as a simple monochrome framebuffer,   and  one  of  the
  218. bits selects whether the 8-bit color or the 1-bit monochrome
  219. image is visible at this pixel.  In this case,   the  normal
  220. choice would be to export two Visuals, a depth 8 PseudoColor
  221. Visual and a depth 1 StaticGray Visual.  Even  more  complex
  222. hardware  may  need  to export many Visuals to allow clients
  223. full access to its capabilities.
  224.  
  225. If you're a sneaky server implementor,  you may even  export
  226. Visuals  that  you  don't strictly have.  For example,  both
  227. the MIT server for  the  DEC  QDSS  color  display  and  the
  228. X11/NeWS  server  for  Sun  color hardware export both Pseu-
  229. doColor and  StaticColor  Visuals  even  thought  they  both
  230. really  have  only  a  PseudoColor display.  The StaticColor
  231. Visuals use otherwise unused entries in the hardware  color-
  232. map,   allowing  simple color applications to achieve better
  233. colormap sharing than they would  on  a  single  PseudoColor
  234. Visual.
  235.  
  236. What this means to you as an X client programmer is that you
  237. are likely to be faced with a choice of Visuals, and that as
  238. hardware evolves to give you bigger and better thrills, this
  239. choice  is  likely  to expand greatly.  You have to choose a
  240. suitable Visual for your client, and unless you choose  with
  241. great  care, you will be increasingly at risk for hallucino-
  242. genic bugs.
  243.  
  244. Hallucinogenic Bugs and Their Symptoms
  245.  
  246. Hallucinogenic bugs achieve  their  effects  by  interfering
  247. with the Visual mechanism.  Here's a list of the common hal-
  248. lucinogens and their symptoms:
  249.  
  250. +  A program can assume that the default Visual is the  only
  251.    available Visual.
  252.  
  253.    Supose you had a color application that made this assump-
  254.    tion  and  a  Sun  3/110C.  The server implementors might
  255.    have chosen to make the  depth-1  monochrome  Visual  the
  256.    default,   in  which case the application would fail even
  257.    though the hardware actually supported color.
  258.  
  259. +  A program can assume that all Visuals with more than  two
  260.    Colormap cells are color.
  261.  
  262.    Suppose you  had  a  color  application  that  made  this
  263.    assumption  and  a  Sun  3/160GS  (grey-scale).  It might
  264.    chose a foreground and a background color which mapped to
  265.    the  same shade of grey,  and the output would be invisi-
  266.    ble.
  267.  
  268. +  A program can assume that all Visuals with more than  two
  269.    Colormap cells have writable Colormaps.
  270.  
  271.    This is a very common assumption.  Clients making it fail
  272.    with  an  Access error when they try to write a read-only
  273.    colormap cell.
  274.  
  275. +  A program can assume that Colormaps (and  especially  the
  276.    default Colormap) are infinitely large,  so that attempts
  277.    to allocate private cells in them will always succeed.
  278.  
  279.    Clients that make this  assumption  will  sometimes  fail
  280.    with  Alloc  errors,  and sometimes succeed, depending on
  281.    the number of cells they ask for and the number of  cells
  282.    that other clients have left available in the colormap.
  283.  
  284. +  A program can assume that all  Colormaps  work  with  all
  285.    Visuals.
  286.  
  287.    Clients that install Colormaps in windows other than  the
  288.    one  they  were  created for may get a Match error if the
  289.    Visuals don't correspond.
  290.  
  291. Traditional Values Keep Your Colors Sober
  292.  
  293. At this point,  you're probably saying ``this  isn't  a  big
  294. deal, all my friends do bugs like this''.  You're wrong; the
  295. only clients that can ignore the  question  of  Visuals  are
  296. those  that  use the BlackPixel() and WhitePixel() macros to
  297. paint a black and white image in the  default  Visual.   All
  298. other  clients must pay some attention to the details of the
  299. Visual(s) they are using,  if  they  want  their  output  to
  300. appear in sober, everyday hues.
  301.  
  302. When you choose a color,  you really choose an  RGB  triple.
  303. For  example,   you  say  ``I'd  like  this text to come out
  304. blue'', and what you really mean  is  that  you'd  like  the
  305. image  of  the  text  when its refreshed on to the screen to
  306. have the RGB triple [001].  Working backwards:
  307.  
  308. +  this means that the colormap cell  used  in  the  refresh
  309.    process must have the RGB triple [001] in it,
  310.  
  311. +  and this means that the pixel value in  the  window  must
  312.    have  indexed to a cell in the colormap with the RGB tri-
  313.    ple [001] in it,
  314.  
  315. +  and this means that the foreground pixel value in the  GC
  316.    that  you  used  to draw the text must have been one that
  317.    would index to a cell with the RGB triple [001] in it,
  318.  
  319. +  and this means that the foreground pixel value  you  sup-
  320.    plied  to a CreateGC or ChangeGC call that got you the GC
  321.    that you used to draw the text must have  been  one  that
  322.    would index to a cell with the RGB triple [001] in it,
  323.  
  324. OK,  enough of the "for the lack of a nail the horseshoe was
  325. lost"  stuff.  You need to have some way to convert your RGB
  326. value into a pixel value that will map  back  into  the  RGB
  327. value.
  328.  
  329. Where Do Pixel Values Come From?
  330.  
  331. I expect you've overheard whispered conversations among your
  332. friends  about  where pixel values come from and how you can
  333. get one.  I'm here to dispel the myths,  and assure you that
  334. there are just three legal ways you can get a pixel value:
  335.  
  336. +  You can give the server a colormap and an RGB value, or a
  337.    text name for an RGB value, and ask it to give you back a
  338.    pixel value that will index to a read-only cell  in  that
  339.    colormap  that  has  the closest available match for that
  340.    RGB value.
  341.  
  342. +  You can ask the server to reserve you a  private,   writ-
  343.    able  cell  in  a Colormap.  If this succeeds, the server
  344.    will give you a pixel value for your  private  cell,  and
  345.    you can set whatever RGB value you like.
  346.  
  347. +  Or there are various ways in  which  you  can  compute  a
  348.    pixel  value  from an RGB triple by predicting the values
  349.    in the colormap:
  350.  
  351.    +  If the server has a TrueColor Visual it  will  provide
  352.       linear ramps.
  353.  
  354.    +  The  so-called  ``Standard  Colormaps''  also  provide
  355.       linear ramps.
  356.  
  357.    +  The connection  handshake  process  tells  you  Black-
  358.       Pixel() and WhitePixel() for the default Visual.
  359.  
  360. Be warned that values you obtain any other way are  illegal,
  361. and may be bugs.
  362.  
  363. Which Method Is Right For You?
  364.  
  365. +  For beginners,  and the simplest clients,   using  Black-
  366.    Pixel() and WhitePixel() in the default Visual is best.
  367.  
  368. +  Using read-only colormap cells and letting the server  do
  369.    the  conversion  will  work  on  any Visual and maximises
  370.    sharing with other  clients.   It  is  the  technique  of
  371.    choice unless:
  372.  
  373.    +  you have a lot of colors,
  374.  
  375.    +  the exact  representation  of  colors  is  of  primary
  376.       importance to you,
  377.  
  378.    +  or you want to play colormap tricks.
  379.  
  380. +  Clients that want to  display  an  RGB  image  with  many
  381.    colors  should use the prediction technique in one of the
  382.    Standard Colormaps, as soon as the experts can  agree  on
  383.    how to make the technique really work.
  384.  
  385. +  Colormap tricks  aren't  actually  illegal,  but  they're
  386.    risky  and  we  advise against them.  If you want to play
  387.    these tricks you should make  sure  beforehand  that  you
  388.    have  a  dynamic Visual, and create a private Colormap so
  389.    as not to disturb your neighbours.
  390.  
  391. Are You Safe if You Use a Toolkit?
  392.  
  393. I expect you'll hear people saying ``I don't have  to  worry
  394. about  this kind of bug,  I use a Toolkit''.  Unfortunately,
  395. this is just another of the myths about bugs.  Not  that  we
  396. don't advise people to use Toolkits; used in accordance with
  397. the manufacturer's instructions a Toolkit can keep you  safe
  398. from  many common bugs and save you a great deal of time and
  399. trouble.  But the sad fact is that  Toolkits  don't  protect
  400. against  hallucinogenic bugs, and this hasn't had the publi-
  401. city it deserves - the  Intrinsics  manual  doesn't  mention
  402. Visuals.
  403.  
  404. How Do Widgets Get Their Visuals?
  405.  
  406. At present,  Visuals are hereditary - Widgets inherit  their
  407. Visuals  from  their  parents.   The  window for a Widget is
  408. created when the Widget is realized, and the Visual  has  to
  409. be  bound  to  the  window  then.   At  the  root  of  every
  410. application's tree of Widgets is a Shell Widget; the Intrin-
  411. sics  define this to inherit its Visual from its parent (ie.
  412. the root window),  so it will have the default Visual.   All
  413. the  Widgets  on the X11R3 tape have realize procedures that
  414. inherit their Visuals from their parents, so that by  induc-
  415. tion all Widgets have the default Visual.
  416.  
  417. If you're creating a Widget that others may use, it is  your
  418. responsibility  to  ensure  that  it gets a suitable Visual.
  419. Simply trusting your parent to do this  for  you  is  not  a
  420. suitable way to discharge this responsibility
  421.  
  422. How Can You Protect Your Widgets From Hallucinogens?
  423.  
  424. You can do this by equipping them with  a  suitable  realize
  425. procedure.   Based  on  your knowledge of the color require-
  426. ments of the Widget you are defining,  you should choose one
  427. of  the legal methods for obtaining pixel values.  Then, you
  428. should write a realize procedure that:
  429.  
  430. +  Ranks the Visuals in  preference  order,  and  finds  the
  431.    smallest  best Visual that will do the job.  Code to do a
  432.    similar task is in the paper.
  433.  
  434. +  Obtains a suitable Colormap  for  the  conversion  method
  435.    selected,  by:
  436.  
  437.    +  Using one of the Colormap  description  properties  on
  438.       the  root  window,  if  you're  using  the  prediction
  439.       method.
  440.  
  441.    +  Choosing the default Colormap,  if that  will  do  the
  442.       job.
  443.  
  444.    +  Creating a private Colormap using the selected  Visual
  445.       and the root window of the screen.
  446.  
  447. +  Creates a window in the selected  Visual,   and  supplies
  448.    the selected Colormap as one of its attributes.
  449.  
  450. +  Sets the window and the colormap into the core attributes
  451.    of the Widget.
  452.  
  453. Remember,  educating your Widgets about Visuals  right  from
  454. the start is the key to keeping them bug-free for life.
  455.  
  456. Don't Let Your Widgets Accept Colormaps From Strangers
  457.  
  458. You're probably asking  ``If  colormaps  are  attributes  of
  459. Widgets,   why shouldn't I let them be Resources that can be
  460. changed by the user?'' The problem is that the bright  shiny
  461. colormap  that the nice user-man is offering your Widget may
  462. be a hallucinogenic bug.  It might not belong  to  the  same
  463. Visual as your Widget's window, and when the trusting little
  464. Widget tries to set the window's colormap attribute,   wham!
  465. it gets a Match error.
  466.  
  467. Unfortunately,  there is no way to test a  colormap  before-
  468. hand to see if it is compatible with the window whose attri-
  469. bute you're trying to set.  Given a colormap ID,  the proto-
  470. col  doesn't provide a way to find the Visual it was created
  471. for.  So,  its better to let your Widgets choose  their  own
  472. colormaps,  and not to provide any way for the user to over-
  473. ride their choice.
  474.  
  475. If you have to accept a colormap from someone else, and  you
  476. don't  know the Visual, you can create your own colormap for
  477. the Visual you want, read all the entries from  the  strange
  478. map,  and  create  corresponding  entries  in  your own map.
  479. There are problems with this approach:
  480.  
  481. +  The entries have to be read back to the client  and  then
  482.    shipped  back  to the server,  because there is no way of
  483.    copying entries between colormaps of different Visuals.
  484.  
  485. +  There is no way of discovering which cells in the  source
  486.    colormap are sharable and which private.  This means that
  487.    the  create-and-copy  destroys  the  sharability  of  the
  488.    entries.
  489.  
  490. +  The whole idea of creating a new colormap  works  against
  491.    the sharing of resources that was probably the reason for
  492.    trying to accept a strange colormap in the first place.
  493.  
  494. How You Can Get Your Programs Off Bugs
  495.  
  496. +  Understand the X display and Visual mechanism.
  497.  
  498. +  Understand the legal methods of converting RGB triples to
  499.    pixel values.
  500.  
  501. +  Chose a method and a Visual that match your application.
  502.  
  503. +  Don't let someone else choose your Visual for you.
  504.  
  505. +  Don't accept colormaps from strangers.
  506.  
  507. What To Do If You Find Bugs In Your Program
  508.  
  509. +  Isolate the program immediately.
  510.  
  511. +  Do not pass the program on to others.
  512.  
  513. +  Examine the program's assumptions carefully.
  514.  
  515. +  Call for help from the xperts.
  516.