Search code examples
c++linuxxlibx11

Xlib multiple windows events handling


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
  }
}

Solution

  • 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