Discussion:
X window newbie needs sample code
(too old to reply)
Norm Dresner
2004-11-02 08:09:57 UTC
Permalink
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
moma
2004-11-02 08:29:42 UTC
Permalink
Post by Norm Dresner
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/lupg/tutorials/xlib-programming/xlib-programming.html

For example you compile the "simple_draw.c" program
http://users.actcom.co.il/%7Echoo/lupg/tutorials/xlib-programming/simple-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
Norm Dresner
2004-11-02 23:04:42 UTC
Permalink
Post by moma
Post by Norm Dresner
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
Post by moma
Post by Norm Dresner
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/lupg/tutorials/xlib-programming/xlib-programming.html
Post by moma
For example you compile the "simple_draw.c" program
http://users.actcom.co.il/%7Echoo/lupg/tutorials/xlib-programming/simple-drawing.c
Post by moma
$ 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
Norm Dresner
2004-11-03 10:17:44 UTC
Permalink
Post by moma
Post by Norm Dresner
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
Post by moma
Post by Norm Dresner
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/lupg/tutorials/xlib-programming/xlib-programming.html
Post by moma
For example you compile the "simple_draw.c" program
http://users.actcom.co.il/%7Echoo/lupg/tutorials/xlib-programming/simple-drawing.c
Post by moma
$ 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
Steve Kirkendall
2004-11-03 21:28:04 UTC
Permalink
http://users.actcom.co.il/%7Echoo/lupg/tutorials/xlib-programming/simple-drawing.c
Post by Norm Dresner
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.
Norm Dresner
2004-11-04 19:43:37 UTC
Permalink
http://users.actcom.co.il/%7Echoo/lupg/tutorials/xlib-programming/simple-drawing.c
Post by Steve Kirkendall
Post by Norm Dresner
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
Georg Acher
2004-11-04 20:30:20 UTC
Permalink
Post by Norm Dresner
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, ***@in.tum.de
http://wwwbode.in.tum.de/~acher
"Oh no, not again !" The bowl of petunias
Josh Stern
2004-11-04 21:04:41 UTC
Permalink
Post by moma
http://users.actcom.co.il/%7Echoo/lupg/tutorials/xlib-programming/simple-drawing.c
Post by Steve Kirkendall
Post by Norm Dresner
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.
Post by moma
TIA
Norm
Norm Dresner
2004-11-05 19:47:35 UTC
Permalink
http://users.actcom.co.il/%7Echoo/lupg/tutorials/xlib-programming/simple-drawing.c
Post by Norm Dresner
Post by Steve Kirkendall
Post by Norm Dresner
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);
Post by Norm Dresner
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
Georg Acher
2004-11-05 21:15:24 UTC
Permalink
Post by Norm Dresner
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, ***@in.tum.de
http://wwwbode.in.tum.de/~acher
"Oh no, not again !" The bowl of petunias
Norm Dresner
2004-11-06 19:12:31 UTC
Permalink
Post by Georg Acher
Post by Norm Dresner
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.
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

Josef Moellers
2004-11-02 09:12:12 UTC
Permalink
Post by Norm Dresner
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).
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
Norm Dresner
2004-11-02 21:52:35 UTC
Permalink
Post by Norm Dresner
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
Post by Norm Dresner
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
Jan Knutar
2004-11-02 16:21:27 UTC
Permalink
Post by Norm Dresner
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.
George Peter Staplin
2004-11-04 01:16:57 UTC
Permalink
Post by Norm Dresner
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
Continue reading on narkive:
Loading...