Re: Don't mind if I grouse on some basic topics?? (very long)



Eigenvector wrote:
I know as experienced programmers you really don't want to hear people gripe and moan about basic, indeed critical topics, but I just have to vent on this. I am a relatively experienced C programmer and having dipped my toe in the water of X programming I just can't get my head around two very critical topics.

What is an event? Having read multiple books on this subject, including the O'Reilly series (very good books I might add) none of the texts defined it clearly. As a greenhorn it was aggrevating how indirect the texts are on that subject. I don't have anything off the top of my head to demonstrate but at least let me articulate. Is an event anything that occurs on screen? Is it only actions precipitated by changes to a window or cursor? Is it only a distinct set of actions. Examples don't seem to really help, an actual definition does though.

An 'event' is the commonality between changes to input devices, and changes to windows NOT arising from the window itself. IOW anything that *might* require reaction from the application/ If you want a *rigorous* definition, the best place would be www.x.org and look in the definition of the X protocol.


The second question involves the execution of code within an X framework. Under a normal piece of C code the compiler runs though the code in a linear fashion, jumping to functions as the code demands but it always completes commands within a set sequence. It ALWAYS complete the command before moving to the next command as well. To my very untrained eyes that isn't the case within X. With X it appears that the executable is waiting for certain external triggers or actions before executing code, and even then it doesn't appear to uniformly execute them. I do understand that graphics are buffered and whatnot - but do they HAVE to be buffered, what if you wanted to produce raw output unbuffered? I don't see any evidence that something like that is even remotely possible within X.

Yes there is. It's called 'synchronous mode', see XSynchronize(). And yes, it tends to be *so* slow that asynchronous is the default. In that mode, its not that the commands are buffered, they are *sent* ASAP, but the client does not wait for the completion until it needs an event.

That's why I see the difference between C code and X C code, in C the commands trigger one, two, three, four, exit. In X it's almost like it stacks the commands up then does them all simultaneously. Can that behavior be removed? Can it be altered reliably?

Yes, but it's definitely not recommended.


I'm sorry to unload like that, I expect it to behave like C behaves, but it doesn't it. It drives me crazy because I use C syntax and C compilers to code it, but it doesn't execute like C!

It's a client/server protocol. Dont try to bend it to your idea, it wont work.


How about a code fragment to give me a way in. And I do apologize for dumping this on you.

I won't include the whole thing, as X has an incredible amount of overhead.


/****************************************/
win=XCreateSimpleWindow(dpy, parent, 10, 10, 200, 200, 5, black, white);
/* I can reliably create windows */
XMapWindow(dpy, win);

gc=XCreateGC(dpy, win, mask, &values);
while(1)
{
XNextEvent(dpy, &event)
switch(event.type)
{
case Expose: /* Whatever that means!!!*/
draw_points();
break;
case ButtonPress: /*Click mouse and terminate program?????? */
exit(1);
default:
break;
}
}

void draw_points(void)
{
XDrawPoint(dpy, win, gc, 20, 20);
sleep (1);
XDrawPoint(dpy, win, gc, 40, 40);
}


Here is what I WANT this code to do. I would like to simply open a window and have the computer draw points, shapes, lines, colors on the screen. I don't need text, I don't need mouse interaction, I don't need a scrollbar. Open window and draw some points. To me this should be trivial stuff indeed. As a matter of fact I can't figure out what the heck XNextEvent is even doing for me. Why can't I just do without the whole while loop and call the draw_points function directly? But I can't, without it the points don't appear. To me this gets back to the whole idea of exposure events, buffering, and the differences between C code and X code.

That's why you should use a toolkit (Motif or Gtk or QT). With that, you could draw just once in a pixmap, and put it in a label (using Motif). Be aware, though, that programs that draw often *do* have a callback for expose events (because the odds are that you dont draw the same thing all the time) and *do* some form of buffering.





--
Michel Bardiaux
R&D Director
T +32 [0] 2 790 29 41
F +32 [0] 2 790 29 02
E mailto:mbardiaux@xxxxxxxxxxx

Mediaxim NV/SA
Vorstlaan 191 Boulevard du Souverain
Brussel 1160 Bruxelles
http://www.mediaxim.com/
.