Correction de ascenseu.c

/* ascenseur */

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

Display *display; 
int     screen; 
Window  root, barre, ascenseur; 
unsigned long white_pixel, black_pixel;
 
main() { 
  if ((display = XOpenDisplay ("")) == NULL) { 
    fprintf (stderr, "Can't open Display\n"); 
    exit (1); 
  } 
  screen = DefaultScreen (display); 
  root = RootWindow (display, screen); 
  white_pixel = WhitePixel (display, screen); 
  black_pixel = BlackPixel (display, screen); 
  barre = XCreateSimpleWindow (display, root, 0, 0, 400, 40, 2, 
			       black_pixel, white_pixel); 
  XSelectInput (display, barre, ButtonMotionMask | ButtonPressMask |
		PointerMotionHintMask );
  XStoreName(display, barre, "ascenseu");
  ascenseur = XCreateSimpleWindow (display, barre, 0, 0, 40, 40, 0, 
			       white_pixel, black_pixel); 
  XMapSubwindows (display, barre); 
  XMapWindow (display, barre); 

  for (;;) { 
    XEvent ev; 
    int xasc, xsouris, bidon;
    Window wbidon;

    XNextEvent (display, &ev); 
    switch (ev.type) { 
    case ButtonPress : 
      if (ev.xbutton.subwindow != ascenseur) {
	if (ev.xbutton.x > 360)
	  xasc = 360;
	else
	  xasc = ev.xbutton.x - 20;
	XMoveWindow (display, ascenseur, xasc, 0);
      }
      break;
    case MotionNotify :
      XQueryPointer (display, barre, &wbidon, &wbidon, &bidon, &bidon, 
		     &xsouris,
		     &bidon, &bidon);
      xasc = xsouris - 20;
      if (xasc < 0)
	xasc = 0 ;
      if (xasc > 380)
	xasc = 360;
      XMoveWindow (display, ascenseur, xasc, 0);
      break;
    default:
      break;
    }
  } 
} 

 

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