/* Initialization code to talk to the X server */ #include #include #include #include #define BORDER_WIDTH 2 /* Global Variables */ Display *theDisplay; int theScreen; int theDepth; unsigned long theBlackPixel; unsigned long theWhitePixel; Colormap theColormap; unsigned long pixel; GC thDestinationGC; unsigned long theplane,width,height; Window theWindow, openWindow(); GC theGC; XImage *ximage; // Establised a connection to the X server nitX() { theDisplay = XOpenDisplay( NULL); /* Check for connection */ if( theDisplay== NULL) { fprintf(stderr, "Error :- %s \n", XDisplayName(NULL)); exit(1); } //set default setting theScreen = DefaultScreen(theDisplay); theDepth = DefaultDepth(theDisplay, theScreen ); theBlackPixel = BlackPixel(theDisplay , theScreen); theWhitePixel = WhitePixel(theDisplay , theScreen); theColormap = DefaultColormap(theDisplay,theScreen); }//End of nitX Function /* getxinfo prints information */ getXInfo() { printf("%s version %d of the X window system , X %d R %d \n ", ServerVendor(theDisplay),VendorRelease(theDisplay), ProtocolVersion(theDisplay),ProtocolRevision(theDisplay)); if(theDepth == 1) { printf("color planes depth...........%d(monochrome)\n",theDepth); } else { printf("color plane depth ............%d\n",theDepth); } printf("Display width..............%d\n",DisplayWidth(theDisplay ,theScreen)); printf("Display heightwidth..............%d\n",DisplayHeight(theDisplay ,theScreen)); }/*End Of Function GetXinfo */ //quitx function to destroy the window quitX() { XCloseDisplay(theDisplay); }//End of quit function /*window function code*/ Window openWindow( int x , int y , int width , int height, int flag, GC *theNewGC) { XSetWindowAttributes theWindowAttributes; XSizeHints theSizeHints; unsigned long theWindowMask; Window theNewWindow; XWMHints theWMHints; theWindowAttributes.border_pixel = BlackPixel(theDisplay,theScreen); theWindowAttributes.background_pixel = WhitePixel(theDisplay,theScreen); theWindowAttributes.override_redirect = True; theWindowMask = CWBackPixel | CWBorderPixel | CWOverrideRedirect; theNewWindow = XCreateWindow(theDisplay, RootWindow(theDisplay,theScreen) ,x,y,width,height,BORDER_WIDTH,theDepth,InputOutput,CopyFromParent, theWindowMask,&theWindowAttributes); theWMHints.initial_state = NormalState; XSetWMHints(theDisplay ,theNewWindow ,&theWMHints); theSizeHints.flags = PPosition |PSize; theSizeHints.x = x; theSizeHints.y = y; theSizeHints.width = width; theSizeHints.height = height; XSetNormalHints(theDisplay ,theNewWindow, &theSizeHints); if(createGC (theNewWindow ,theNewGC ) == 0) { XDestroyWindow(theDisplay,theNewWindow ); return((Window) 0); } XMapWindow(theDisplay ,theNewWindow); return(theNewWindow); }//End of Window function createGC(Window theNewWindow , GC *theNewGC) { XGCValues theGCValues; *theNewGC = XCreateGC(theDisplay, theNewWindow,(unsigned long ) 0 ,&theGCValues); if(*theNewGC == 0 ) { return(0); } else { XSetForeground(theDisplay, *theNewGC ,theBlackPixel); XSetBackground(theDisplay, *theNewGC ,theWhitePixel); return(1); } }//end of creategc //Set Color Function setColorWithName(GC theGC,char theName[]) { XColor theRGBColor,theHardwareColor; int theStatus; if (theDepth>1) { theStatus = XLookupColor(theDisplay,theColormap, theName,&theRGBColor,&theHardwareColor); if(theStatus != 0) { theStatus = XAllocColor(theDisplay,theColormap, &theHardwareColor); if(theStatus != 0) { XSetForeground(theDisplay,theGC, theHardwareColor.pixel); } } } }//End of set Color function main() { nitX(); getXInfo(); /* u can change the width and height of the window */ theWindow = openWindow(100,100,300,300,0, &theGC); //set the color if required setColorWithName(theGC,"blue"); // call your function /* Get the pixel value to changed it to other value(basically used for flood fill or Boundary fill algorithms */ ximage= XGetImage(theDisplay,theWindow,0,0,300,300,AllPlanes,XYPixmap); pixel=XGetPixel(ximage,10,10); printf("Pixel value is :- %u",pixel); /* call for the function ( flood fill or boundary fill) */ sleep(2); /* Distroy the window and quit*/ XDestroyWindow(theDisplay ,theWindow); quitX(); }//end of main //Command to Compile the XWindow File // $cc fin.c -L /usr/X11R6/lib -lX11 -lm