/* Initialization code to talk to the X server */ #include #include #include #include # include #define BORDER_WIDTH 2 #define MAX_COLOR 10 /* Global Variables */ Display *theDisplay; int theScreen; int theDepth; unsigned long theBlackPixel; unsigned long theWhitePixel; Colormap theColormap; int theStatus; XColor theRGBColor,theHardwareColor; unsigned long pixel; GC thDestinationGC; unsigned long theplane,width,height; Window theWindow, openWindow(); GC theGC; XImage *ximage; char BrColorName[10]; char FiColorName[10]; unsigned long fillcr,bcr; // 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 /* Sets the foreground color using name of the color */ setColorWithName(char theN[]) { if (theDepth>1) { theStatus = XLookupColor(theDisplay,theColormap, theN,&theRGBColor,&theHardwareColor); if(theStatus != 0) { theStatus = XAllocColor(theDisplay,theColormap, &theHardwareColor); if(theStatus != 0) { XSetForeground(theDisplay,theGC, theHardwareColor.pixel); } } } }//End of setColorWithName /* This function sets the foreground color using Pixel value of the color */ setColorWithValue(unsigned long pixelval) { if (theDepth>1) { if(theStatus != 0) { theStatus = XAllocColor(theDisplay,theColormap, &theHardwareColor); if(theStatus != 0) { XSetForeground(theDisplay,theGC, pixelval); } } } } /* This function takes name of the color and returns the corresponding pixel value */ int getColorValue( char theName[]) { if (theDepth>1) { theStatus = XLookupColor(theDisplay,theColormap, theName,&theRGBColor,&theHardwareColor); if(theStatus != 0) { theStatus = XAllocColor(theDisplay,theColormap, &theHardwareColor); } } return(theHardwareColor.pixel); } void chooseColor() { printf("\n Border Color name:- "); scanf("%s",BrColorName); printf("\n Fill Color name:- "); scanf("%s",FiColorName); bcr=getColorValue(BrColorName); fillcr=getColorValue(FiColorName); } int main() { nitX(); getXInfo(); /* u can change the width and height of the window */ theWindow = openWindow(100,100,300,300,0, &theGC); chooseColor();//specify the color and set the value //setColorWithName(BrColorName); //XDrawRectangle (theDisplay,theWindow,theGC,10,10,30,30); /* 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); //call for the function(flood fill or boundary fill) sleep(2); /* Distroy the window and quit*/ XDestroyWindow(theDisplay ,theWindow); quitX(); return(0); }//end of main