Window does not receive expose events and therefore is not redrawn

Marcus Reinhard marcus at gedanken-schmiede.de
Wed Dec 9 10:24:45 PST 2015


Hello everybody,
I have written a little test-app because I want to develop an Xlib-based 
app and didn't want to jump right in. It showed that this was a good 
idea because I got stuck right at the start... :-(
-My problem is, that my code works as intended except for the part where 
the window should be redrawn... :-(
To be more specific, the window is drawn correctly at first, but altough 
I see the lines in the terminal showing the mouse-/keyboard-input, the 
window itself is not redrawn, and if I force this by 
minimizing/restoring the window, the symbol (here a simple line) showing 
which section was selected with the arrow-keys, is not drawn except when 
the selection is back at the first section which is the default-selection...

here's the code

#include<iostream>
#include<X11/Xlib.h>
#include<unistd.h>

int main(int argc,char** argv)
{
   Display *display=XOpenDisplay(NULL);
   Visual *visual=DefaultVisual(display,0);
   Window 
window=XCreateSimpleWindow(display,RootWindow(display,0),0,0,500,500,0,0,0);
   GC gc=DefaultGC(display,0);
   XMapWindow(display,window);
XSelectInput(display,window,ButtonPressMask|ExposureMask|KeyPressMask|StructureNotifyMask);
   XFlush(display);
   XEvent xev;
   bool run=true;
   int auswahl=1;

   while(run) {
     XNextEvent(display,&xev);
     switch(xev.type) {
     case Expose:
       XClearWindow(display,xev.xany.window);
       //XFlush(display);
       XSetForeground(display,gc,0xffffffff);
       XDrawString(display,window,gc,50,50,"Test1",5);
       XDrawLine(display,window,gc,0,75,499,75);
       XDrawString(display,window,gc,50,100,"Test2",5);
       XDrawLine(display,window,gc,0,125,499,125);
       XDrawString(display,window,gc,50,150,"Test3",5);
XDrawLine(display,window,gc,150,auswahl*50-25,200,auswahl*50-25);
       //XFlush(display);
       std::cout<<"Auswahl="<<auswahl<<" 
count="<<xev.xexpose.count<<std::endl;
       break;
     case ButtonPress:
       std::cout<<"ButtonPress at 
("<<xev.xbutton.x<<","<<xev.xbutton.y<<")"<<std::endl;
       if(xev.xbutton.y<75) std::cout<<"Test1"<<std::endl;
       else if(xev.xbutton.y>75 && xev.xbutton.y<125) 
std::cout<<"Test2"<<std::endl;
       else if(xev.xbutton.y>125) std::cout<<"Test3"<<std::endl;
XDrawLine(display,window,gc,250,250,xev.xbutton.x,xev.xbutton.y);
       break;
     case KeyPress:
std::cout<<"KeyPress:"<<xev.xkey.keycode<<std::endl;
       switch(xev.xkey.keycode) {
       case 9: //ESC
     std::cout<<"ESC"<<std::endl;
     run=false;
     break;
       case 111: //rauf
     std::cout<<"hoch";
     auswahl--;
     if(auswahl==0) auswahl=3;
     std::cout<<auswahl<<std::endl;
     break;
       case 116: //runter
     std::cout<<"runter";
     auswahl++;
     if(auswahl==4) auswahl=1;
     std::cout<<auswahl<<std::endl;
     break;
       }
       //XFlush(display);
       //XSync(display,false);
       //XMapWindow(display,window);
       //XClearWindow(display,window);
       break;
     default:
       std::cout<<"unrecognized Event "<<xev.type<<std::endl;
     }
     std::cout<<"."<<std::endl;
   }
   return 0;
}


-I have read every (and I mean every) page, google suggest for this 
problem, but none really did help me... -so every help/hint/suggestion 
is very much appreciated :-)

-Marcus


More information about the xorg mailing list