Correction de arrplan.c

/* change la couleur d'arriere plan de la fenetre selon la lettre tapee n/b */

#include <stdio.h>
#include <X11/Xlib.h>

GC      gc; 
Display *display; 
int     screen; 
Window  win, root; 
unsigned long white_pixel, black_pixel; 
XSetWindowAttributes attrib;
unsigned long masq_attrib;
int nbre;
char chaine[20];
KeySym touche;
 
main() {
  if ((display = XOpenDisplay ("")) == NULL) { 
    fprintf (stderr, "Can't open Display\n"); 
    exit (1); 
  } 
  gc = DefaultGC (display, screen); 
  screen = DefaultScreen (display); 
  root = RootWindow (display, screen); 
  white_pixel = WhitePixel (display, screen); 
  black_pixel = BlackPixel (display, screen); 
  win = XCreateSimpleWindow (display, root,  
			     0, 0, 100, 90, 2, black_pixel, white_pixel); 
  XSelectInput (display, win, ExposureMask | KeyPressMask); 
  XStoreName (display, win, "arrplan"); 
  masq_attrib = CWBackPixel;
  XMapWindow (display, win); 
 
  for (;;) { 
    XEvent ev; 
    XNextEvent (display, &ev); 
    switch (ev.type) { 
    case Expose :  
      XDrawString (display, win, gc, 10, 30, "coucou !", 8);  
      break; 
    case KeyPress :
      nbre = XLookupString(&ev, chaine, 20, &touche, 0);
      chaine[nbre] = 0;
      if ((nbre == 1) && (chaine[0] == 'q'))
	exit(0);
      if (nbre == 1) {
      if (chaine[0] == 'n')
	attrib.background_pixel = black_pixel;
      if (chaine[0] == 'b')
	attrib.background_pixel = white_pixel;
      XChangeWindowAttributes (display, win, masq_attrib, &attrib);
      XClearWindow (display, win);
      }
    default : 
      break;            
    } 
  } 
} 
 

chapitre de l'exercice index général fonctions de la XLib événements types dans XLib