X window newbie needs sample code - Xwindows

This is a discussion on X window newbie needs sample code - Xwindows ; I'm a very experienced (real-time) C programmer in a variety of OS with a few sophisticated user-space C++ programs under my belt as well in Windows including several moderately complex graphics programs, but I've never written a single X window ...

+ Reply to Thread
Results 1 to 15 of 15

Thread: X window newbie needs sample code

  1. X window newbie needs sample code

    I'm a very experienced (real-time) C programmer in a variety of OS with a
    few sophisticated user-space C++ programs under my belt as well in Windows
    including several moderately complex graphics programs, but I've never
    written a single X window graphics program of any sort.

    I have a need to write a single X-window graphics program which constructs a
    rectangular pixel array on the screen and internally assigns each pixel to
    an element of a memory array. It then needs to monitor the contents of this
    array by changing the colors of the associated pixels on the screen. If the
    array size were small, I'd do this with (n)curses and characters but the
    array is 32K and there aren't that many character positions available on any
    reasonable shell window. While this might sound like a real-time
    application, it's really very soft real-time with a (nominal) 1 Hz update
    rate.

    Ideally I'd like to find some sample code that operates on a pixel-by-pixel
    basis and writes pixels of arbitrary color to random places on the screen.
    Any pointers (URL's or book references) appreciated.

    TIA
    Norm


  2. Re: X window newbie needs sample code

    Norm Dresner wrote:
    > I'm a very experienced (real-time) C programmer in a variety of OS with a
    > few sophisticated user-space C++ programs under my belt as well in Windows
    > including several moderately complex graphics programs, but I've never
    > written a single X window graphics program of any sort.
    >
    > I have a need to write a single X-window graphics program which constructs a
    > rectangular pixel array on the screen and internally assigns each pixel to
    > an element of a memory array. It then needs to monitor the contents of this
    > array by changing the colors of the associated pixels on the screen. If the
    > array size were small, I'd do this with (n)curses and characters but the
    > array is 32K and there aren't that many character positions available on any
    > reasonable shell window. While this might sound like a real-time
    > application, it's really very soft real-time with a (nominal) 1 Hz update
    > rate.
    >
    > Ideally I'd like to find some sample code that operates on a pixel-by-pixel
    > basis and writes pixels of arbitrary color to random places on the screen.
    > Any pointers (URL's or book references) appreciated.
    >
    > TIA
    > Norm
    >


    Programming with XLib
    http://users.actcom.co.il/%7Echoo/lu...ogramming.html

    For example you compile the "simple_draw.c" program
    http://users.actcom.co.il/%7Echoo/lu...mple-drawing.c

    $ gcc simple-drawing.c -o simple-drawing -L/usr/X11R6/lib -lX11

    Run it
    $ ./simple_draw

    Other resources
    http://www.rahul.net/kenton/xsites.html
    http://lesstif.sourceforge.net/doc/Xlib/spec/xlib.html


    // moma
    http://www.futuredesktop.org/how2burn.html#mirrors
    http://www.futuredesktop.org/OpenOffice.html

  3. Re: X window newbie needs sample code

    Norm Dresner wrote:
    > I'm a very experienced (real-time) C programmer in a variety of OS witha
    > few sophisticated user-space C++ programs under my belt as well in Windows
    > including several moderately complex graphics programs, but I've never
    > written a single X window graphics program of any sort.
    >
    > I have a need to write a single X-window graphics program which constructs a
    > rectangular pixel array on the screen and internally assigns each pixelto
    > an element of a memory array. It then needs to monitor the contents ofthis
    > array by changing the colors of the associated pixels on the screen. If the
    > array size were small, I'd do this with (n)curses and characters but the
    > array is 32K and there aren't that many character positions available on any
    > reasonable shell window. While this might sound like a real-time
    > application, it's really very soft real-time with a (nominal) 1 Hz update
    > rate.
    >
    > Ideally I'd like to find some sample code that operates on a pixel-by-pixel
    > basis and writes pixels of arbitrary color to random places on the screen.
    > Any pointers (URL's or book references) appreciated.


    Where does the data come from? Maybe you can use Perl/Tk which has the
    additional advantage of being (quite) portable between Linux and Windows
    (ActiveState Perl).

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


  4. Re: X window newbie needs sample code

    Norm Dresner wrote:

    > Ideally I'd like to find some sample code that operates on a
    > pixel-by-pixel basis and writes pixels of arbitrary color to random places
    > on the screen. Any pointers (URL's or book references) appreciated.


    If you just want a quick and dirty framebuffer, try libSDL. As a bonus,
    it's also portable.

  5. Re: X window newbie needs sample code

    "Josef Moellers" wrote in message
    news:cm7is3$j1h$1@nntp.fujitsu-siemens.com...
    Norm Dresner wrote:
    > I'm a very experienced (real-time) C programmer in a variety of OS with a
    > few sophisticated user-space C++ programs under my belt as well in Windows
    > including several moderately complex graphics programs, but I've never
    > written a single X window graphics program of any sort.
    >
    > I have a need to write a single X-window graphics program which constructs

    a
    > rectangular pixel array on the screen and internally assigns each pixel to
    > an element of a memory array. It then needs to monitor the contents of

    this
    > array by changing the colors of the associated pixels on the screen. If

    the
    > array size were small, I'd do this with (n)curses and characters but the
    > array is 32K and there aren't that many character positions available on

    any
    > reasonable shell window. While this might sound like a real-time
    > application, it's really very soft real-time with a (nominal) 1 Hz update
    > rate.
    >
    > Ideally I'd like to find some sample code that operates on a

    pixel-by-pixel
    > basis and writes pixels of arbitrary color to random places on the screen.
    > Any pointers (URL's or book references) appreciated.


    Where does the data come from? Maybe you can use Perl/Tk which has the
    additional advantage of being (quite) portable between Linux and Windows
    (ActiveState Perl).

    The data comes via ioctl from a device driver I wrote. What I'm actually
    doing is indirectly monitoring some Dual Ported Memory on an ISA bus card
    because is driver is almost certainly overwriting part of it but because
    that's part of a suite of 11 drivers and user programs, we don't know where
    it's being done and we're hoping that by finding out when we'll get a clue
    to where. BTW, my driver is part of a suite of 11 drivers and 12 programs
    that are also simultaneously running -- and yes, we need that many
    drivers -- the computer is a rackmount with a 16-slot ISA bus backplane
    that's almost full.

    Unfortunately, while I've read some things about Perl, I've never used it
    either.

    Norm


  6. Re: X window newbie needs sample code

    "moma" wrote in message
    news:WzHhd.4788$rh1.126096@news2.e.nsc.no...
    > Norm Dresner wrote:
    > > I'm a very experienced (real-time) C programmer in a variety of OS with

    a
    > > few sophisticated user-space C++ programs under my belt as well in

    Windows
    > > including several moderately complex graphics programs, but I've never
    > > written a single X window graphics program of any sort.
    > >
    > > I have a need to write a single X-window graphics program which

    constructs a
    > > rectangular pixel array on the screen and internally assigns each pixel

    to
    > > an element of a memory array. It then needs to monitor the contents of

    this
    > > array by changing the colors of the associated pixels on the screen. If

    the
    > > array size were small, I'd do this with (n)curses and characters but the
    > > array is 32K and there aren't that many character positions available on

    any
    > > reasonable shell window. While this might sound like a real-time
    > > application, it's really very soft real-time with a (nominal) 1 Hz

    update
    > > rate.
    > >
    > > Ideally I'd like to find some sample code that operates on a

    pixel-by-pixel
    > > basis and writes pixels of arbitrary color to random places on the

    screen.
    > > Any pointers (URL's or book references) appreciated.
    > >
    > > TIA
    > > Norm
    > >

    >
    > Programming with XLib
    >

    http://users.actcom.co.il/%7Echoo/lu...ogramming.html
    >
    > For example you compile the "simple_draw.c" program
    >

    http://users.actcom.co.il/%7Echoo/lu...mple-drawing.c
    >
    > $ gcc simple-drawing.c -o simple-drawing -L/usr/X11R6/lib -lX11
    >
    > Run it
    > $ ./simple_draw
    >
    > Other resources
    > http://www.rahul.net/kenton/xsites.html
    > http://lesstif.sourceforge.net/doc/Xlib/spec/xlib.html
    >
    >
    > // moma


    Absolutely perfect recommendations. The simple_draw program should be all I
    need to craft my DPM monitoring program.

    Thanks a million
    Norm


  7. X window newbie needs help with sample code


    "moma" wrote in message
    news:WzHhd.4788$rh1.126096@news2.e.nsc.no...
    > Norm Dresner wrote:
    > > I'm a very experienced (real-time) C programmer in a variety of OS with

    a
    > > few sophisticated user-space C++ programs under my belt as well in

    Windows
    > > including several moderately complex graphics programs, but I've never
    > > written a single X window graphics program of any sort.
    > >
    > > I have a need to write a single X-window graphics program which

    constructs a
    > > rectangular pixel array on the screen and internally assigns each pixel

    to
    > > an element of a memory array. It then needs to monitor the contents of

    this
    > > array by changing the colors of the associated pixels on the screen. If

    the
    > > array size were small, I'd do this with (n)curses and characters but the
    > > array is 32K and there aren't that many character positions available on

    any
    > > reasonable shell window. While this might sound like a real-time
    > > application, it's really very soft real-time with a (nominal) 1 Hz

    update
    > > rate.
    > >
    > > Ideally I'd like to find some sample code that operates on a

    pixel-by-pixel
    > > basis and writes pixels of arbitrary color to random places on the

    screen.
    > > Any pointers (URL's or book references) appreciated.
    > >
    > > TIA
    > > Norm
    > >

    >
    > Programming with XLib
    >

    http://users.actcom.co.il/%7Echoo/lu...ogramming.html
    >
    > For example you compile the "simple_draw.c" program
    >

    http://users.actcom.co.il/%7Echoo/lu...mple-drawing.c
    >
    > $ gcc simple-drawing.c -o simple-drawing -L/usr/X11R6/lib -lX11
    >
    > Run it
    > $ ./simple_draw
    >


    I have a problem with this program. It runs just fine but there's
    nothing drawn in the window! I've even tried adding to the program by
    drawing a line a few pixels thick and nothing shows up in the window.

    Is there some function call to set the color of the drawn objects that's
    missing?

    TIA
    Norm



  8. Re: X window newbie needs help with sample code

    Norm Dresner wrote:
    >
    > "moma" wrote in message
    > >

    http://users.actcom.co.il/%7Echoo/lu...mple-drawing.c
    >
    > I have a problem with this program. It runs just fine but there's
    > nothing drawn in the window! I've even tried adding to the program by
    > drawing a line a few pixels thick and nothing shows up in the window.
    >
    > Is there some function call to set the color of the drawn objects
    > that's missing?


    The unmodified program works for me. It explicitly sets the foreground
    and background colors when it creates the graphic context, by calling
    the XSetForeground() and XSetBackground() functions from inside its
    creage_gc() function. It uses the WhitePixel() and BlackPixel() macros
    to get the color values. If your program needs more colors, see the
    XAllocNamedColor() function.

    By any chance, did you omit the XFlush() call? It's sort of like
    refresh() in ncurses -- it sends the output queue to the display
    device (X server), and is needed only in programs that don't
    periodically wait for input. Without XFlush(), the drawing requests
    could stay in the queue until the program exits, so you'd never see
    them.

  9. Re: X window newbie needs sample code

    on Norm wrote:
    > I have a need to write a single X-window graphics program which constructs a
    > rectangular pixel array on the screen and internally assigns each pixel to
    > an element of a memory array. It then needs to monitor the contents of this
    > array by changing the colors of the associated pixels on the screen. If the
    > array size were small, I'd do this with (n)curses and characters but the
    > array is 32K and there aren't that many character positions available on any
    > reasonable shell window. While this might sound like a real-time
    > application, it's really very soft real-time with a (nominal) 1 Hz update
    > rate.
    >
    > Ideally I'd like to find some sample code that operates on a pixel-by-pixel
    > basis and writes pixels of arbitrary color to random places on the screen.
    > Any pointers (URL's or book references) appreciated.


    I suggest that you use the XShm/MIT-SHM extension for a pixmap and XDBE
    for double buffering.

    xsrc/xc/doc/specs/Xext/mit-shm.ms

    In that directory use:
    nroff -ms ../../util/macros.t mit-shm.ms | more

    man DBE should help with XDBE.


    Basically the theory behind an XShm Pixmap is that you allocate some
    memory with shmget(), tell X about that memory, and share/attach-to the
    memory using the shmat() function. Then whenever you want to update the
    Pixmap you manipulate the array of memory that you attached to.

    XDBE will help by making redraws sharp, instead of with visible
    different chunks (as is all too common with some X programs that use
    Pixmaps and XCopyArea large areas to a Window). You can copy the XShm
    Pixmap to an XdbeBackBuffer and then swap the buffers for display.

    The downside is that XShm doesn't work over the network. You can
    however write an alternative (slower) code path that uses XCreateImage
    and XPutImage for networked displays.


    Best wishes,

    George

  10. Re: X window newbie needs help with sample code

    "Steve Kirkendall" wrote in message
    news:4189520a@nntp0.pdx.net...
    > Norm Dresner wrote:
    > >
    > > "moma" wrote in message
    > > >

    >

    http://users.actcom.co.il/%7Echoo/lu...mple-drawing.c
    > >
    > > I have a problem with this program. It runs just fine but there's
    > > nothing drawn in the window! I've even tried adding to the program by
    > > drawing a line a few pixels thick and nothing shows up in the window.
    > >
    > > Is there some function call to set the color of the drawn objects
    > > that's missing?

    >
    > The unmodified program works for me. It explicitly sets the foreground
    > and background colors when it creates the graphic context, by calling
    > the XSetForeground() and XSetBackground() functions from inside its
    > creage_gc() function. It uses the WhitePixel() and BlackPixel() macros
    > to get the color values. If your program needs more colors, see the
    > XAllocNamedColor() function.
    >
    > By any chance, did you omit the XFlush() call? It's sort of like
    > refresh() in ncurses -- it sends the output queue to the display
    > device (X server), and is needed only in programs that don't
    > periodically wait for input. Without XFlush(), the drawing requests
    > could stay in the queue until the program exits, so you'd never see
    > them.


    Nope. The code I downloaded from that URL has a call to XFlush(display);
    immediately before the sleep(3) at the end of the program. I get nothing in
    the box. I'm using RedHat 7.2 with gcc 2.95.3 and the script file I'm using
    to compile and link is
    #!/bin/tcsh
    gcc -o simple -L/usr/X11R6/lib -lX11 simple.c
    simple
    I get a perfectly normal looking window with a white background and not a
    single pixel is anything other than white inside the frame.

    What else could I do to check this out and make it work?

    TIA
    Norm



  11. Re: X window newbie needs help with sample code

    "Norm Dresner" writes:

    >Nope. The code I downloaded from that URL has a call to XFlush(display);
    >immediately before the sleep(3) at the end of the program. I get nothing in


    Have you tried XSync(Display)?

    --
    Georg Acher, acher@in.tum.de
    http://wwwbode.in.tum.de/~acher
    "Oh no, not again !" The bowl of petunias

  12. Re: X window newbie needs help with sample code

    On Thu, 04 Nov 2004 19:43:37 +0000, Norm Dresner wrote:

    > "Steve Kirkendall" wrote in message
    > news:4189520a@nntp0.pdx.net...
    >> Norm Dresner wrote:
    >> >
    >> > "moma" wrote in message
    >> > >

    >>

    > http://users.actcom.co.il/%7Echoo/lu...mple-drawing.c
    >> >
    >> > I have a problem with this program. It runs just fine but there's
    >> > nothing drawn in the window! I've even tried adding to the program by
    >> > drawing a line a few pixels thick and nothing shows up in the window.
    >> >
    >> > Is there some function call to set the color of the drawn objects
    >> > that's missing?

    >>
    >> The unmodified program works for me. It explicitly sets the foreground
    >> and background colors when it creates the graphic context, by calling
    >> the XSetForeground() and XSetBackground() functions from inside its
    >> creage_gc() function. It uses the WhitePixel() and BlackPixel() macros
    >> to get the color values. If your program needs more colors, see the
    >> XAllocNamedColor() function.
    >>
    >> By any chance, did you omit the XFlush() call? It's sort of like
    >> refresh() in ncurses -- it sends the output queue to the display
    >> device (X server), and is needed only in programs that don't
    >> periodically wait for input. Without XFlush(), the drawing requests
    >> could stay in the queue until the program exits, so you'd never see
    >> them.

    >
    > Nope. The code I downloaded from that URL has a call to XFlush(display);
    > immediately before the sleep(3) at the end of the program. I get nothing in
    > the box. I'm using RedHat 7.2 with gcc 2.95.3 and the script file I'm using
    > to compile and link is
    > #!/bin/tcsh
    > gcc -o simple -L/usr/X11R6/lib -lX11 simple.c
    > simple
    > I get a perfectly normal looking window with a white background and not a
    > single pixel is anything other than white inside the frame.
    >
    > What else could I do to check this out and make it work?


    The program works for me. Things you could try:

    1) execute ./simple to make sure you are running the
    correct binary
    2) print out the value of BlackPixel and check
    what that value should look like on your screen with
    a color chooser.


    > TIA
    > Norm



  13. Re: X window newbie needs help with sample code -- PARTIAL RESOLUTION

    > >> Norm Dresner wrote:
    > >> >
    > >> > "moma" wrote in message
    > >> > >
    > >>

    > >

    http://users.actcom.co.il/%7Echoo/lu...mple-drawing.c
    > >> >
    > >> > I have a problem with this program. It runs just fine but

    there's
    > >> > nothing drawn in the window! I've even tried adding to the program

    by
    > >> > drawing a line a few pixels thick and nothing shows up in the window.
    > >> >
    > >> > Is there some function call to set the color of the drawn objects
    > >> > that's missing?
    > >>
    > >> The unmodified program works for me. It explicitly sets the foreground
    > >> and background colors when it creates the graphic context, by calling
    > >> the XSetForeground() and XSetBackground() functions from inside its
    > >> creage_gc() function. It uses the WhitePixel() and BlackPixel() macros
    > >> to get the color values. If your program needs more colors, see the
    > >> XAllocNamedColor() function.
    > >>
    > >> By any chance, did you omit the XFlush() call? It's sort of like
    > >> refresh() in ncurses -- it sends the output queue to the display
    > >> device (X server), and is needed only in programs that don't
    > >> periodically wait for input. Without XFlush(), the drawing requests
    > >> could stay in the queue until the program exits, so you'd never see
    > >> them.

    > >
    > > Nope. The code I downloaded from that URL has a call to

    XFlush(display);
    > > immediately before the sleep(3) at the end of the program. I get

    nothing in
    > > the box. I'm using RedHat 7.2 with gcc 2.95.3 and the script file I'm

    using
    > > to compile and link is
    > > #!/bin/tcsh
    > > gcc -o simple -L/usr/X11R6/lib -lX11 simple.c
    > > simple
    > > I get a perfectly normal looking window with a white background and not

    a
    > > single pixel is anything other than white inside the frame.
    > >
    > > What else could I do to check this out and make it work?


    Okay -- here's the deal [so far].
    I downloaded and compiled and linked the program from the URL above
    exactly the way is says to do it.
    The window came up blank [white background].
    Following suggestions, I tried some things and here's the result
    XSync did nothing.
    I _was_ executing the program in the CWD -- ./simple wasn't necessary.
    This was easy to prove by adding a printf or two.
    The values of the Black and White Pixels were 0x0000000 & 0x0000FFFF
    which were perfectly reasonable.
    The original program had an XFlush() before the final sleep so the queue
    was definitely flushed.

    SO ...
    I added after the window-creation and after each group of output calls
    #if TRACE_IT
    XFlush( display );
    fprintf( stderr , ... ) ;
    sleep( 2 );
    #endif
    and the window suddenly had a drawing in it.

    Going back and disabling these sections one at a time finally found a
    single sleep(1) that was absolutely necessary: the one after the call to
    the function that creates the GC. I'll try shorter delays using other
    methods to see exactly how much is really necessary. I've tried inserting
    calls to XFlush() and XSync() after the creation and they don't make any
    difference at all.
    If all I have to do to make _my_ program work is to add a single
    sleep(1) call at the beginning, then I have absolutely no problems with this
    and I'm on my way. BUT ... if it's indicative of a deeper problem, then
    I'll be back.

    Thanks much to all who tried to help.
    Norm




  14. Re: X window newbie needs help with sample code -- PARTIAL RESOLUTION

    "Norm Dresner" writes:

    > Going back and disabling these sections one at a time finally found a
    >single sleep(1) that was absolutely necessary: the one after the call to
    >the function that creates the GC. I'll try shorter delays using other
    >methods to see exactly how much is really necessary. I've tried inserting
    >calls to XFlush() and XSync() after the creation and they don't make any
    >difference at all.
    > If all I have to do to make _my_ program work is to add a single
    >sleep(1) call at the beginning, then I have absolutely no problems with this
    >and I'm on my way. BUT ... if it's indicative of a deeper problem, then
    >I'll be back.


    That solution might work, but you have definitively a problem. The demo program
    lacks any event processing, which is very bad. Simple demos are fine, but they
    should contain no serious bugs...

    Even XFlush/XSync doesn't guarantee when the window is actually appearing. And if
    you draw in a non-mapped window (no matter if you've issued XMapWindow before),
    you'll get an empty window. Due to the interference with the window manager
    (which has a hook for XMapWindow to add it's decoration), XFlush doesn't help
    here, you really need to wait for the mapping.

    Try the following sequence:

    XSelectInput (dpy, win, ExposureMask);
    XMapWindow (dpy, win);
    for (;
    {
    XEvent event;
    XNextEvent(dpy, &event);
    if (event.type == Expose) break;
    }
    // Now do your drawing and finish with XSync/XFlush

    That should wait until the window is actually mapped.

    --
    Georg Acher, acher@in.tum.de
    http://wwwbode.in.tum.de/~acher
    "Oh no, not again !" The bowl of petunias

  15. Re: X window newbie needs help with sample code -- FINAL RESOLUTION

    "Georg Acher" wrote in message
    news:cmgqhc$itv$1@wsc10.lrz-muenchen.de...
    > "Norm Dresner" writes:
    >
    > > Going back and disabling these sections one at a time finally found a
    > >single sleep(1) that was absolutely necessary: the one after the call to
    > >the function that creates the GC. I'll try shorter delays using other
    > >methods to see exactly how much is really necessary. I've tried

    inserting
    > >calls to XFlush() and XSync() after the creation and they don't make any
    > >difference at all.
    > > If all I have to do to make _my_ program work is to add a single
    > >sleep(1) call at the beginning, then I have absolutely no problems with

    this
    > >and I'm on my way. BUT ... if it's indicative of a deeper problem, then
    > >I'll be back.

    >
    > That solution might work, but you have definitively a problem. The demo

    program
    > lacks any event processing, which is very bad. Simple demos are fine, but

    they
    > should contain no serious bugs...
    >
    > Even XFlush/XSync doesn't guarantee when the window is actually appearing.

    And if
    > you draw in a non-mapped window (no matter if you've issued XMapWindow

    before),
    > you'll get an empty window. Due to the interference with the window

    manager
    > (which has a hook for XMapWindow to add it's decoration), XFlush doesn't

    help
    > here, you really need to wait for the mapping.
    >
    > Try the following sequence:
    >
    > XSelectInput (dpy, win, ExposureMask);
    > XMapWindow (dpy, win);
    > for (;
    > {
    > XEvent event;
    > XNextEvent(dpy, &event);
    > if (event.type == Expose) break;
    > }
    > // Now do your drawing and finish with XSync/XFlush
    >
    > That should wait until the window is actually mapped.
    >


    BINGO!!

    That's perfect. And I'm starting to understand more of the X Window
    system too. I've got Volume 1 of the O'Reilly series and I'm into the 2nd
    chapter reading it. I've got most of the rest of the series on order and by
    the time Monday rolls around I should have a working application to debug
    with the real equipment.

    Thanks a million to all who tried to help me.
    Norm


+ Reply to Thread