Discussion:
Program startup problem
(too old to reply)
Norm Dresner
2004-11-16 19:06:31 UTC
Permalink
After much coaxing by some very knowledgeable people, I finally got my first
X-window program to start properly by putting a wait-loop for an
ExposureEvent immediately after the call to XMapWindow(). That worked fine
on my personal development system but when I got the program to the heavily
loaded test bench, the program didn't start. Putting checkpoint printf()
after each X-window call, I found that the program had passed through the
wait-loop and was hung in my own Refresh() function which (re)writes the
screen.

Here's extracted code -- not all declarations or error checking is shown --
and this program _does_ work perfectly both on my development system and on
the test bench when it's (more-or-less) idle. It only fails to initialize
properly when the test bench system is heavily loaded. I first found that
the program initialized properly when I single-stepped it through the
debugger (gdb) and eventually deduced that I could simulate that by
inserting a sleep() at the indicated line. Sleeping for only 2 seconds
wasn't sufficient and 3 seconds wasn't always enough either but the 4-second
sleep seemed to work all the time. Obviously I'm still missing something
and I'm hoping that some experienced X-window programmers will spot it.

I'd appreciate any suggestions.
TIA
Norm

display = XOpenDisplay(display_name);
win = create_simple_window(display, width, height, 0, 0);
XSelectInput( display , win , ExposureMask );
XMapWindow(display, win);
XFlush(display);
while( TRUE )
{
XEvent event;
XNextEvent( display , &event );
if( event.type == Expose )
break;
}
}
<== sleep(4) inserted here
gc = create_gc(display, win);
XSync(display, False);
XFlush( display );
<== program hangs here as described above
Refresh();
Norm Dresner
2004-11-16 20:34:07 UTC
Permalink
Fred

I didn't intend to display the entire program but just the startup
routine! There _is_ another event loop later on with real events and real
handling. I have been told that I had to wait for the ExposeEvent in order
to have anything displayed and before I did it the window was always blank
and afterwards the program displayed correctly -- on the development system

Norm
Post by Norm Dresner
After much coaxing by some very knowledgeable people, I finally got my first
X-window program to start properly by putting a wait-loop for an
ExposureEvent immediately after the call to XMapWindow(). That worked fine
on my personal development system but when I got the program to the heavily
loaded test bench, the program didn't start. Putting checkpoint printf()
after each X-window call, I found that the program had passed through the
wait-loop and was hung in my own Refresh() function which (re)writes the
screen.
Here's extracted code -- not all declarations or error checking is shown --
and this program _does_ work perfectly both on my development system and on
the test bench when it's (more-or-less) idle. It only fails to initialize
properly when the test bench system is heavily loaded. I first found that
the program initialized properly when I single-stepped it through the
debugger (gdb) and eventually deduced that I could simulate that by
inserting a sleep() at the indicated line. Sleeping for only 2 seconds
wasn't sufficient and 3 seconds wasn't always enough either but the 4-second
sleep seemed to work all the time. Obviously I'm still missing something
and I'm hoping that some experienced X-window programmers will spot it.
I'd appreciate any suggestions.
TIA
Norm
display = XOpenDisplay(display_name);
win = create_simple_window(display, width, height, 0, 0);
XSelectInput( display , win , ExposureMask );
XMapWindow(display, win);
XFlush(display);
while( TRUE )
{
XEvent event;
XNextEvent( display , &event );
if( event.type == Expose )
break;
}
}
<== sleep(4) inserted here
gc = create_gc(display, win);
XSync(display, False);
XFlush( display );
<== program hangs here as described above
Refresh();
Using sleep() is almost always wrong, and usually means that your
program design is extremely flawed. For most X programs, the main loop
is an infinite loop that merely has two statements - one to get the next
event, and one to dispatch that event. Note that in your loop, you get
events and ignore all of them, including the Expose event you break on!
What it the end goal of your program? Just placing a window on the
screen isn't very useful. And using only Xlib is going to make it very
tedious for you to do anything.
You should at least be using Xt (or better yet, Motif, Athena, or some
other higher-level toolkit.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Fred L. Kleinschmidt
2004-11-16 20:29:23 UTC
Permalink
Post by Norm Dresner
After much coaxing by some very knowledgeable people, I finally got my first
X-window program to start properly by putting a wait-loop for an
ExposureEvent immediately after the call to XMapWindow(). That worked fine
on my personal development system but when I got the program to the heavily
loaded test bench, the program didn't start. Putting checkpoint printf()
after each X-window call, I found that the program had passed through the
wait-loop and was hung in my own Refresh() function which (re)writes the
screen.
Here's extracted code -- not all declarations or error checking is shown --
and this program _does_ work perfectly both on my development system and on
the test bench when it's (more-or-less) idle. It only fails to initialize
properly when the test bench system is heavily loaded. I first found that
the program initialized properly when I single-stepped it through the
debugger (gdb) and eventually deduced that I could simulate that by
inserting a sleep() at the indicated line. Sleeping for only 2 seconds
wasn't sufficient and 3 seconds wasn't always enough either but the 4-second
sleep seemed to work all the time. Obviously I'm still missing something
and I'm hoping that some experienced X-window programmers will spot it.
I'd appreciate any suggestions.
TIA
Norm
display = XOpenDisplay(display_name);
win = create_simple_window(display, width, height, 0, 0);
XSelectInput( display , win , ExposureMask );
XMapWindow(display, win);
XFlush(display);
while( TRUE )
{
XEvent event;
XNextEvent( display , &event );
if( event.type == Expose )
break;
}
}
<== sleep(4) inserted here
gc = create_gc(display, win);
XSync(display, False);
XFlush( display );
<== program hangs here as described above
Refresh();
Using sleep() is almost always wrong, and usually means that your
program design is extremely flawed. For most X programs, the main loop
is an infinite loop that merely has two statements - one to get the next
event, and one to dispatch that event. Note that in your loop, you get
events and ignore all of them, including the Expose event you break on!

What it the end goal of your program? Just placing a window on the
screen isn't very useful. And using only Xlib is going to make it very
tedious for you to do anything.
You should at least be using Xt (or better yet, Motif, Athena, or some
other higher-level toolkit.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Loading...