[comp.windows.x] --- fundamental Xlib problem

umesh@csd4.csd.uwm.edu (Umesh N Gadasalli) (02/10/90)

Hi netters,

  I am just starting out on X-lib and I have a basic fundamental
  problem. Any help will be much appreciated.

  I have an Event Gathering Loop which handles the events KeyPress
  as well as ButtonPress. I have an application where the user presses
  a particular key ( say 'B' ) which starts off execution of a loop. I need  
  this loop to be executed continuosly untill the user hits another 
  particular key ( say 'S' ).

  The present code is something like this:

    while (1)
    {
      XNextEvent( display, &event );
      switch( event )
      {
       case Expose: -----
		    break;

       case ButtonPress:---
			break;

        case KeyPress:
		      i = XLookupString(............);

		      do                    /*  see       */
		      {                     /* comments   */
		       EXECUTE();           /*  below     */
		      }                     /*
		      until ( key == 'B' ); /*
      }
    }


     comments:

     when i hit a 'B' the program starts executing something.
     i want this to be continued until i hit 'S'
     How do I sense that 'S' was pressed when I am within
     the loop ?

     umesh@csd4.csd.uwm.edu

garfinke@hpfcda.HP.COM (Dan Garfinkel) (02/12/90)

>   I have an Event Gathering Loop which handles the events KeyPress
>   as well as ButtonPress. I have an application where the user presses
>   a particular key ( say 'B' ) which starts off execution of a loop. I need  
>   this loop to be executed continuosly untill the user hits another 
>   particular key ( say 'S' ).
>
>     when i hit a 'B' the program starts executing something.
>     i want this to be continued until i hit 'S'
>     How do I sense that 'S' was pressed when I am within
>     the loop ?

Here are three options:

1) Put an XPending() call in your Execute loop and break out of your loop to 
process the events. 

2)  If you don't want your output to flush (for performance reasons), you 
could use a combination of the QLength macro and select() yourself to see 
if there is input pending. 

3) Use 2 clients, one which handles input and the other which does output, 
and send signals (or some other ipc) between them.


Hope this helps,

Dan Garfinkel, HP

rowe@cme.nist.gov (Walter Rowe) (02/13/90)

Here is another possible solution for processing until some event has
occurred (ie. until the `S' key has been pressed):

======================================================================
SYNTAX
     Bool XCheckIfEvent(display, event_return, predicate, arg)
           Display *display;
           XEvent *event_return;
           Bool (*predicate)();
           char *arg;

ARGUMENTS
     arg       Specifies the user-supplied argument that will be
               passed to the predicate procedure.

     display   Specifies the connection to the X server.

     event_return
               Returns either a copy of or  the matched event's
               associated structure.

     predicate Specifies the procedure that is to be called to
               determine if the next event in the queue matches
               what you want.

DESCRIPTION
     When the predicate procedure finds a match, XCheckIfEvent
     copies the matched event into the client-supplied XEvent
     structure and returns True.  (This event is removed from the
     queue.) If the predicate procedure finds no match, XCheckI-
     fEvent returns False, and the output buffer will have been
     flushed.  All earlier events stored in the queue are not
     discarded.
======================================================================

So, inside your processing loop, add a call to XCheckIfEvent():

	while (! XCheckIfEvent (...)) {
	   what_ever_code_goes_here;
	}

I used this for an X-based talk program so you can conference calls
with multiple users on multiple displays.  I wanted to poll various
displays/windows in a round robin fashion looking for input from one
user that needed to be distributed that as output to the other users
(so they can see what you just typed).

-Walter Rowe  <rowe@cme.nist.gov>

hvr@kimba.Sun.COM (Heather Rose) (02/16/90)

In article <ROWE.90Feb13093044@norman.cme.nist.gov> rowe@cme.nist.gov (Walter Rowe) writes:
>Here is another possible solution for processing until some event has
>occurred (ie. until the `S' key has been pressed):

If you want to use a toolkit, XView supplies a function called 
notify_do_dispatch which will process window events while waiting for
input on a file descriptor (like a user typing 'S' to stdin).

I already posted code about this subject to this list, so I will not clutter 
it again with a repeat.  See the on line XView examples for an example.

If you don't want to use the XView UI, it is still possible to use the
XView notifier as an input distribution mechanism.

Caveat: notify_do_dispatch requires that your OS has syscall() since it
	replaces the default select() and read() system calls with its own
	version.

Heather