Monday, December 30, 2013

Mapping - UnMapping A XWindow

                Because of race condition lot of people face problems in Mapping or UnMapping  a particular Xwindow and all of its sub-windows that have had map / unmap requests in X11R6. In this post I am going to show you how to Map and UnMap a Xwindow based on MapNotify and UnmapNotify events.



 /* FileName: Map_UnMap.cpp */

#include <X11/Xlib.h>
#include <assert.h> 
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#define NIL (0)

int main()
{

      Display *dpy = XOpenDisplay(NIL);
      assert(dpy);

      int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
      int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));


      Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
                     200, 100, 0, blackColor, blackColor);

      XSelectInput(dpy, w, StructureNotifyMask);

      XMapWindow(dpy, w);
      sleep(2);
      printf( "Mapping : : 0x%x\n", (unsigned int)w);
      int count=1;
           
      GC gc = XCreateGC(dpy, w, 0, NIL);
      XSetForeground(dpy, gc, whiteColor);

      XEvent event;

      while ( 1 ) {
        XNextEvent( dpy, &event );
        if ( event.type == MapNotify ) {
            XMapEvent *mapevent = (XMapEvent *)&event;
            printf( "UnMapping    : 0x%x\n", (unsigned int)(mapevent->window) );
            ++count;
            XUnmapWindow(dpy, w);
            sleep(2);
        }

    if ( event.type == UnmapNotify ) {
            XUnmapEvent *unmapwindowevent = (XUnmapEvent *)&event;
            printf( "Mapping : 0x%x\n", (unsigned int)(unmapwindowevent->window) );
            ++count;
            XMapWindow(dpy, w);
            sleep(2);
        }
           if(count==10)
              break;

        if ( event.type == DestroyNotify ) {
            XDestroyWindowEvent *destroywindowevent = (XDestroyWindowEvent *)&event;
            printf( "Destroyed : 0x%x\n", (unsigned int)(destroywindowevent->window) );
        }
    }

      XFlush(dpy);
      sleep(3);
      return 0;
}

To Run: g++ Map_UnMap.cpp -lX11
             ./a.out

No comments:

Post a Comment