home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / doc / tutorials / answers.txt next >
Encoding:
Text File  |  1991-08-26  |  11.3 KB  |  225 lines

  1.              Late Night's Top Ten X11 Questions
  2.  
  3.              Dave Lemke (lemke@ncd.com)
  4.                Stuart Marks (smarks@sun.com)
  5.  
  6. 1.  Why doesn't my program draw anything?  I create the window and draw into
  7.     it, but it's always blank.
  8.  
  9.     In order to get a window on the screen, you must map it first, using the
  10.     XMapWindow() call.  Drawing into a window and then mapping it won't work
  11.     in most cases.
  12.  
  13. 1a. But I do map the window before drawing into it, and the window is still
  14.     blank.
  15.  
  16.     The problem here is that if the window is a child of the root window,
  17.     XMapWindow() doesn't actually map the window; it sends a message to the
  18.     window manager asking it to map the window.  If you having drawing
  19.     requests right after the XMapWindow() call, they will probably be
  20.     processed before the window manager has actually mapped the window.  In
  21.     order to get around this delayed mapping effect, you must wait for an
  22.     Exposure event from the server.  This event indicates that your window is
  23.     on the screen and ready to receive graphics.  In general, you should
  24.     always draw in response to Exposure events.
  25.  
  26. 2.  Why does my program display in inverse-video on some servers?
  27.  
  28.     You're assuming that 1 is black and 0 is white or vice-versa.  The
  29.     relationship between pixels 0 and 1 and the colors black and white is
  30.     implementation-dependent.  They may be reversed, or they may not even
  31.     correspond to black and white at all.  For instance, an 8-bit grayscale
  32.     server might choose to have 0 be black and 255 be white.  If you want to
  33.     draw in black and white, set the foreground and background pixels in the
  34.     GC using BlackPixel() and WhitePixel() macros instead of 0 and 1.  This
  35.     will help ensure that your application is portable amongst different
  36.     servers.
  37.  
  38. 3.  Can I get the background pixel/pixmap of a window using
  39.     GetWindowAttributes?
  40.  
  41.     No.  Once set, the background pixel or pixmap of a window cannot be
  42.     re-read by clients.  The reason for this is that a client can create a
  43.     pixmap, set it to be the background pixmap of a window, and then free the
  44.     pixmap.  The window keeps this background, but the pixmap itself is
  45.     destroyed.  If you're sure a window has a background pixel (not a
  46.     pixmap), you can use XClearArea() to clear a region to the background
  47.     color and then use XGetImage() to read back that pixel.  However, this
  48.     action alters the contents of the window, and it suffers from race
  49.     conditions with exposures.
  50.  
  51. 4.  Why doesn't my program get keystrokes?  I select input for
  52.     KeyPress events, but my program never gets them.  (Alternatively,
  53.     my program works under uwm, but not under ...)
  54.  
  55.     The window manager controls how the input focus is transferred from one
  56.     window to another.  In order to get keystrokes, your program must ask the
  57.     window manager for the input focus.  To do this, you must set up what are
  58.     called "hints" for the window manager.  If your applications is
  59.     Xlib-based, you can use something like the following:
  60.  
  61.     XWMHints wmhints;
  62.     ...
  63.     wmhints.flags = InputHint;
  64.     wmhints.input = True;
  65.     XSetWMHints(dpy, window, &hints)
  66.  
  67.     If your application is based on the Xt Intrinsics, you can use something
  68.     like the following:
  69.  
  70.     Arg arglist[MAXARGS];
  71.     Cardinal nargs = (Cardinal) 0;
  72.     ...
  73.     XtSetArg(arglist[nargs++], XtNinput, TRUE);
  74.     ...
  75.     XtCreatePopupShell(...);
  76.  
  77. 5.  I encountered a breakpoint when I was debugging a program, and now
  78.     everything is hung.  I can still move the mouse around on the screen,
  79.     but I can't type anything or get any mouse clicks to do anything.  HELP!
  80.  
  81.     The debugger stopped your program while it was in the middle of what is
  82.     called a "grab".  There are various flavors of grabs, but the important
  83.     effect of all of them is that the grabbing client gets serviced at the
  84.     expense of other clients in the system.  This kind of feature is
  85.     necessary for doing highly interactive things like pop-up menu tracking.
  86.  
  87.     The problem arises because the program you're debugging grabbed some
  88.     server resource, such as the keyboard, and then hit a breakpoint.  The
  89.     debugger is sharing the same resource with the debugged program, which
  90.     has grabbed the keyboard away from the debugger.  You can't type
  91.     debugger commands to continue the program, and thus you're stuck.
  92.  
  93.     To get around this problem, you might try to iconify the debugged
  94.     program's window by using the mouse or the keyboard, whichever isn't
  95.     grabbed.  When the window goes iconic, any grabs on that window will be
  96.     deactivated by the server.
  97.  
  98.     If the debugged program is in the middle of a server grab, you're really
  99.     stuck.  A server grab grabs *all* of the server's resources.  Thus, you
  100.     have to attack the problem by going outside of the server.  Killing the
  101.     debugged program and its debugger will release the grab and get you
  102.     unstuck, but you obviously can't continue debugging.  You might also try
  103.     making a special debugging-mode option to your program so that it doesn't
  104.     grab the server while you're debugging it.  If you can't do that, your
  105.     only choice is to debug the offending program from somewhere outside the
  106.     server it's displaying on, e.g. another workstation or a terminal.  If
  107.     you're running X11/NeWS from Sun, you can run a SunView shelltool on top
  108.     of the server; it will be unaffected by server grabs.
  109.  
  110. 6.  How do I do XOR drawing to work properly?  It doesn't work at all on some
  111.     servers, and I get funny colors on other servers.
  112.  
  113.     It depends on what you mean by "properly."  For the most part, when you
  114.     do XOR drawing, you want to do some transient animation that won't
  115.     disturb the contents of the window.  You can do this with XOR because the
  116.     first XOR operation will change the contents to something different, and
  117.     then the second XOR operation will restore the original contents.  The
  118.     trick is to set up the GC source pixel so that the "something different"
  119.     contrasts properly with the surroundings.
  120.  
  121.     Confusion arises because XOR drawing uses the GC foreground field as the
  122.     source pixel for the XOR operation.  If you use your actual foreground
  123.     pixel as the source for XOR, the result will probably be something off in
  124.     a random section of whatever colormap is installed.  Unless you've set up
  125.     and installed your own colormap, the color you end up with will be some
  126.     unlikely value over which you don't have any control.
  127.  
  128.     What's often appropriate is to interchange the foreground color with the
  129.     background color.  If your graphics has the concept of a foreground and a
  130.     background, you can do XOR animation that switches foreground and
  131.     background by setting the GC foreground pixel to the XOR of the
  132.     foreground and background pixels.  In C, this would be
  133.  
  134.     XGCValues gcvalues;
  135.     gcvalues.foreground = fgpixel ^ bgpixel;
  136.     XChangeGC(dpy,gc,GCForeground,&gcvalues);
  137.  
  138.     For monochrome graphics, you can use
  139.  
  140.     gcvalues.foreground =
  141.     BlackPixel(dpy,DefaultScreen(dpy))^WhitePixel(dpy,DefaultScreen(dpy));
  142.  
  143. 7.  I create and draw into a pixmap, but when I copy it to the screen with
  144.     XCopyArea, I get nothing but garbage.
  145.  
  146.     The initial contents of pixmaps is undefined.  This means that most
  147.     servers will allocate the memory and leave around whatever happens to be
  148.     there -- which is usually garbage.  You probably want to clear the pixmap
  149.     first using XFillRectangle() with an alu function of GXcopy and a
  150.     foreground pixel of whatever color you want as your background (or 0L
  151.     if you are using the pixmap as a mask).
  152.  
  153. 8.  My color program always fails on XAllocColorCells().  But it works on
  154.     other servers.
  155.  
  156.     Your program is assuming that the default colormap for the screen is a
  157.     dynamic one, that is, one out of which you can allocate nonshared color
  158.     cells via XAllocColorCells().  Some servers have a static colormap as
  159.     their default colormap, so XAllocColorCells() will fail.
  160.  
  161.     You have several alternatives.  One possibility is to change your program
  162.     to use XAllocColor().  You won't be able to alter the color once you've
  163.     allocated it, but you may not have needed to do that in the first place.
  164.     You should use XAllocColor() in preference to XAllocColorCells() if you
  165.     simply want to get colors on the screen.  The reason is that
  166.     XAllocColor() allows the same colors to be shared amongst different
  167.     clients.
  168.  
  169.     If you really need to allocate and change colormap cells, then you still
  170.     need to use XAllocColorCells() or XAllocColorPlanes().  For these calls
  171.     to work, you must search the server's visual list for a visual that
  172.     supports dynamic colormaps.  Then, you must create a colormap against
  173.     this visual and allocate colors out of this colormap.  If the server
  174.     doesn't have any dynamic visuals, your program simply won't work on this
  175.     server.  If you successfully create and use your own dynamic colormap,
  176.     you'll also have to create a window using the same visual and set that
  177.     window's colormap attribute to be your newly created colormap.  Don't
  178.     forget to set up WM_COLORMAP_WINDOWS so the window manager can install
  179.     your colormap.
  180.  
  181. 9.  My program worked fine in R3, but fails in X11R4 with an error in
  182.     ChangeWindowAttributes.  Also, my Andrew and Interviews code stopped
  183.     working with the same error.
  184.  
  185.     A likely problem is the value given to a window's do_not_propagate mask.
  186.     R3 allowed bogus values to be set, and early version of both Andrew and
  187.     Interviews did just that.  X11R4 and several productized servers catch
  188.     this error and complain.  The events that can be suppressed are:
  189.  
  190.     KeyPress, KeyRelease, ButtonPress, ButtonRelease, PointerMotion,
  191.     ButtonMotion and Button[1-5]Motion
  192.  
  193. 10. I set my windows to have BackingStore, yet they still get exposures.
  194.  
  195.     There are several things that can be going on here.  The most
  196.     important thing to remember is that backing store is a hint.
  197.     You may have asked for backing store on your window, but the server may
  198.     not have enough memory to service your request.  If this is the case,
  199.     the server will go ahead and create and window without backing store,
  200.     and then send you an Exposure event to tell you to draw into it.
  201.  
  202.     Other behavior that could possibly be counterintuitive is that, if you
  203.     create a window with BackingStore == Always, you will get an Exposure
  204.     event as soon as you create the window.  The server does this to let you
  205.     know that the initial contents of the window need to be filled.  It does
  206.     this even before you have mapped the window.
  207.  
  208.     Some servers will throw away backing store if they start running out of
  209.     memory.  A window may start out with backing store, but then have it
  210.     thrown away by the server because of a memory shortage.  This window
  211.     will then start getting Exposure events.
  212.  
  213.     Another possibility is a window manager with a 'Refresh' function.  Some
  214.     window managers implement this function by doing a ClearArea with the
  215.     Exposure flag set.  This will force the server the send an Exposure
  216.     event, even if the window still has backing store.
  217.  
  218.     The bottom line is that clients must *always* be prepared to repaint
  219.     their windows in response to Exposure events.  Backing store is a hint,
  220.     and it is only useful to reduce the number of Exposures on the window;
  221.     it doesn't guarantee that none will ever occur.  If you need to keep an
  222.     image in the server, but not on the screen, use pixmaps.
  223.  
  224. [@(#)answers.txt 1.3 89/12/15]
  225.