Question

I'm new with xlib. I've have a program which has two displays and two windows. My problem is that when user resizes windows the content disappears.

Both of the windows have turns when one can draw on them. I have while-loops for each window's turn to get the events and handling them. My problem is if I try to listen not-active-window's events with XNextEvent() the program will work randomly. I pasted one of the while-loops below.

I'd really appreciate some help.

while(drawThings2) {
  XNextEvent( dpy2, & e2 );// Get event

  switch( e2.type ){
  case ButtonPress :
     switch( e2.xbutton.button ){
     case Button1 :
        //Start drawing
        break;
     case Button2 :
     case Button3 :
        break;
     }
     break;
  case ButtonRelease :
  switch( e2.xbutton.button ){
      case Button1 :
         //Draw things
        break;
  }
  break;
  case MotionNotify :
     if( drawing && (e2.xmotion.state & Button1Mask) )
     {
        //Draw things
     }
  break;
  case Expose :
     if( e2.xexpose.count >= 0 )
     {
         //Redraw content if current window is resized by user
     }
  break;
  }

  if(XCheckWindowEvent( dpy, w, ExposureMask, & e )>0)
  {
        //Redraw the second window's content if the second window is resized by user
  }
}
Was it helpful?

Solution 2

Thank you for the help! I'll start learning Qt.

I solved my problem by using one big while-loop. In the while-loop I've got two if(XPending(display)) for each display. The program is very simple so I approached with a simple answer and I'm quite freshman so I didn't really understand what's going on with the polling. Here's my solution:

while( !Finished ){
   //If drawing to display1
  if(XPending(dpy)) {
      XEvent e;
      XNextEvent( dpy, & e ); // Get event
      switch( e.type ){
      case ButtonPress :
         switch( e.xbutton.button ){
         case Button1 :
            //Start drawing         
            break;
         case Button2 :
         case Button3 :
            break;
         }
         break;
      case ButtonRelease :
      switch( e.xbutton.button ){
          case Button1 :
            //Draw things
            break;
      }
      break;
      case MotionNotify :
         if( drawing && (e.xmotion.state & Button1Mask) )
         {
            //Draw things
         }
      break;
      case Expose :
         if( e.xexpose.count == 0 ) {
            //Redraw content if current window is resized by user
         }
         break;
      }//switch
  }//if


  else if(XPending(display2)) { //if drawing to display2
    //same things as display1 but changed to display2
  }
}//while

OTHER TIPS

You need to multiplex input (and output) from your two X11 Display-s. In other words, you need a real single event loop using a multiplexing syscall like poll(2) (you should not nest two loops).

I would recommend using a real toolkit (like Qt or GTK), which will provide you with a sophisticated event loop (QApplication and its exec in Qt, gtk_main in Gtk; both toolkits giving a lot of lower level interfaces to their event loops).

If you want to stay with raw Xlib calls (which is painful), poll both display file descriptors (obtained with XConnectionNumber) then use XPeekEvent(3) (on the readable file descriptor[s]).

For an example of event loop with poll see this answer (and adapt it to your needs).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top