kellex

X11lib-CTemplate2

Sep 10th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.35 KB | None | 0 0
  1. /*
  2.  * color-drawing.c - demonstrate drawing of pixels, lines, arcs, etc, using
  3.  *            different foreground colors, in a window.
  4.  */
  5.  
  6. #include <X11/Xlib.h>
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>     /* getenv(), etc. */
  10. #include <unistd.h>     /* sleep(), etc.  */
  11.  
  12. /*
  13.  * function: create_simple_window. Creates a window with a white background
  14.  *           in the given size.
  15.  * input:    display, size of the window (in pixels), and location of the window
  16.  *           (in pixels).
  17.  * output:   the window's ID.
  18.  * notes:    window is created with a black border, 2 pixels wide.
  19.  *           the window is automatically mapped after its creation.
  20.  */
  21. Window
  22. create_simple_window(Display* display, int width, int height, int x, int y)
  23. {
  24.   int screen_num = DefaultScreen(display);
  25.   int win_border_width = 2;
  26.   Window win;
  27.  
  28.   /* create a simple window, as a direct child of the screen's */
  29.   /* root window. Use the screen's black and white colors as   */
  30.   /* the foreground and background colors of the window,       */
  31.   /* respectively. Place the new window's top-left corner at   */
  32.   /* the given 'x,y' coordinates.                              */
  33.   win = XCreateSimpleWindow(display, RootWindow(display, screen_num),
  34.                             x, y, width, height, win_border_width,
  35.                             BlackPixel(display, screen_num),
  36.                             WhitePixel(display, screen_num));
  37.  
  38.   /* make the window actually appear on the screen. */
  39.   XMapWindow(display, win);
  40.  
  41.   /* flush all pending requests to the X server. */
  42.   XFlush(display);
  43.  
  44.   return win;
  45. }
  46.  
  47. GC
  48. create_gc(Display* display, Window win, int reverse_video)
  49. {
  50.   GC gc;                /* handle of newly created GC.  */
  51.   unsigned long valuemask = 0;      /* which values in 'values' to  */
  52.                     /* check when creating the GC.  */
  53.   XGCValues values;         /* initial values for the GC.   */
  54.   unsigned int line_width = 2;      /* line width for the GC.       */
  55.   int line_style = LineSolid;       /* style for lines drawing and  */
  56.   int cap_style = CapButt;      /* style of the line's edje and */
  57.   int join_style = JoinBevel;       /*  joined lines.       */
  58.   int screen_num = DefaultScreen(display);
  59.  
  60.   gc = XCreateGC(display, win, valuemask, &values);
  61.   if (gc < 0) {
  62.     fprintf(stderr, "XCreateGC: \n");
  63.   }
  64.  
  65.   /* allocate foreground and background colors for this GC. */
  66.   if (reverse_video) {
  67.     XSetForeground(display, gc, WhitePixel(display, screen_num));
  68.     XSetBackground(display, gc, BlackPixel(display, screen_num));
  69.   }
  70.   else {
  71.     XSetForeground(display, gc, BlackPixel(display, screen_num));
  72.     XSetBackground(display, gc, WhitePixel(display, screen_num));
  73.   }
  74.  
  75.   /* define the style of lines that will be drawn using this GC. */
  76.   XSetLineAttributes(display, gc,
  77.                      line_width, line_style, cap_style, join_style);
  78.  
  79.   /* define the fill style for the GC. to be 'solid filling'. */
  80.   XSetFillStyle(display, gc, FillSolid);
  81.  
  82.   return gc;
  83. }
  84.  
  85. int main(int argc, char* argv[])
  86. {
  87.   Display* display;     /* pointer to X Display structure.           */
  88.   int screen_num;       /* number of screen to place the window on.  */
  89.   Window win;           /* pointer to the newly created window.      */
  90.   unsigned int display_width,
  91.                display_height;  /* height and width of the X display.        */
  92.   unsigned int width, height;   /* height and width for the new window.      */
  93.   char *display_name = getenv("DISPLAY");  /* address of the X display.      */
  94.   GC gc;            /* GC (graphics context) used for drawing    */
  95.                 /*  in our window.               */
  96.   Colormap screen_colormap;     /* color map to use for allocating colors.   */
  97.   XColor red, brown, blue, yellow, green;
  98.                 /* used for allocation of the given color    */
  99.                 /* map entries.                              */
  100.   Status rc;            /* return status of various Xlib functions.  */
  101.  
  102.   /* open connection with the X server. */
  103.   display = XOpenDisplay(display_name);
  104.   if (display == NULL) {
  105.     fprintf(stderr, "%s: cannot connect to X server '%s'\n",
  106.             argv[0], display_name);
  107.     exit(1);
  108.   }
  109.  
  110.   /* get the geometry of the default screen for our display. */
  111.   screen_num = DefaultScreen(display);
  112.   display_width = DisplayWidth(display, screen_num);
  113.   display_height = DisplayHeight(display, screen_num);
  114.  
  115.   /* make the new window occupy 1/9 of the screen's size. */
  116.   width = (display_width / 3);
  117.   height = (display_height / 3);
  118.   printf("window width - '%d'; height - '%d'\n", width, height);
  119.  
  120.   /* create a simple window, as a direct child of the screen's   */
  121.   /* root window. Use the screen's white color as the background */
  122.   /* color of the window. Place the new window's top-left corner */
  123.   /* at the given 'x,y' coordinates.                             */
  124.   win = create_simple_window(display, width, height, 0, 0);
  125.  
  126.   /* allocate a new GC (graphics context) for drawing in the window. */
  127.   gc = create_gc(display, win, 0);
  128.   XSync(display, False);
  129.  
  130.   /* get access to the screen's color map. */
  131.   screen_colormap = DefaultColormap(display, DefaultScreen(display));
  132.  
  133.   /* allocate the set of colors we will want to use for the drawing. */
  134.   rc = XAllocNamedColor(display, screen_colormap, "red", &red, &red);
  135.   if (rc == 0) {
  136.     fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
  137.     exit(1);
  138.   }
  139.   rc = XAllocNamedColor(display, screen_colormap, "brown", &brown, &brown);
  140.   if (rc == 0) {
  141.     fprintf(stderr, "XAllocNamedColor - failed to allocated 'brown' color.\n");
  142.     exit(1);
  143.   }
  144.   rc = XAllocNamedColor(display, screen_colormap, "blue", &blue, &blue);
  145.   if (rc == 0) {
  146.     fprintf(stderr, "XAllocNamedColor - failed to allocated 'blue' color.\n");
  147.     exit(1);
  148.   }
  149.   rc = XAllocNamedColor(display, screen_colormap, "yellow", &yellow, &yellow);
  150.   if (rc == 0) {
  151.     fprintf(stderr, "XAllocNamedColor - failed to allocated 'yellow' color.\n");
  152.     exit(1);
  153.   }
  154.   rc = XAllocNamedColor(display, screen_colormap, "green", &green, &green);
  155.   if (rc == 0) {
  156.     fprintf(stderr, "XAllocNamedColor - failed to allocated 'green' color.\n");
  157.     exit(1);
  158.   }
  159. /*  
  160.   /* draw one pixel near each corner of the window */
  161.   /* draw the pixels in a red color. */
  162. XSetForeground(display, gc, red.pixel);
  163. XDrawPoint(display, win, gc, 5, 5);
  164. XDrawPoint(display, win, gc, 5, height-5);
  165. XDrawPoint(display, win, gc, width-5, 5);
  166. XDrawPoint(display, win, gc, width-5, height-5);
  167.  
  168.   /* draw two intersecting lines, one horizontal and one vertical, */
  169.   /* which intersect at point "50,100".                            */
  170.   /* draw the line in a brown color. */
  171.   XSetForeground(display, gc, brown.pixel);
  172.   XDrawLine(display, win, gc, 50, 0, 50, 200);
  173.   XDrawLine(display, win, gc, 0, 100, 200, 100);
  174.  
  175.   /* now use the XDrawArc() function to draw a circle whose diameter */
  176.   /* is 30 pixels, and whose center is at location '50,100'.         */
  177.   /* draw the arc in a blue color. */
  178.   XSetForeground(display, gc, blue.pixel);
  179.   XDrawArc(display, win, gc, 50-(30/2), 100-(30/2), 30, 30, 0, 360*64);
  180.  
  181.   {
  182.     XPoint points[] = {
  183.       {0, 0},
  184.       {15, 15},
  185.       {0, 15},
  186.       {0, 0}
  187.     };
  188.     int npoints = sizeof(points)/sizeof(XPoint);
  189.  
  190.     /* draw a small triangle at the top-left corner of the window. */
  191.     /* the triangle is made of a set of consecutive lines, whose   */
  192.     /* end-point pixels are specified in the 'points' array.       */
  193.     /* draw the triangle in a yellow color. */
  194.     XSetForeground(display, gc, yellow.pixel);
  195.     XDrawLines(display, win, gc, points, npoints, CoordModeOrigin);
  196.   }
  197.  
  198.   /* draw a rectangle whose top-left corner is at '120,150', its width is */
  199.   /* 50 pixels, and height is 60 pixels.                                  */
  200.   /* draw the rectangle in a black color. */
  201.   XSetForeground(display, gc, BlackPixel(display, screen_num));
  202.   XDrawRectangle(display, win, gc, 120, 150, 50, 60);
  203.  
  204.   /* draw a filled rectangle of the same size as above, to the left of the */
  205.   /* previous rectangle.                                                   */
  206.   /* draw the rectangle in a green color. */
  207.   XSetForeground(display, gc, green.pixel);
  208.   XFillRectangle(display, win, gc, 60, 150, 50, 60);
  209. */
  210.  
  211. //template
  212.  
  213.  
  214.   /* flush all pending requests to the X server. */
  215.   XFlush(display);
  216.  
  217.   /* make a delay for a short period. */
  218.   sleep(4);
  219.  
  220.   /* close the connection to the X server. */
  221.   XCloseDisplay(display);
  222.   return(0);
  223. }
Advertisement
Add Comment
Please, Sign In to add comment