Advertisement
Guest User

Plot pixels on screen in loop crashes linux

a guest
Nov 28th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cstdlib>
  4. #include <X11/Xlib.h>
  5. #include <X11/Xutil.h>
  6. #include <unistd.h>
  7.  
  8.  
  9. /*
  10. root@ST-LAPTOP:/usr/include/X11# grep "XGetPixel" ./*
  11. ./Xutil.h:extern unsigned long XGetPixel(
  12. ./Xutil.h:#define XGetPixel(ximage, x, y) \
  13. */
  14.  
  15. // Compile: g++ mypointer.c -o my -lX11
  16.  
  17. // cc prog.c -o prog -L/usr/X11/lib -lX11
  18. // or perhaps this (for a system with release 6 of X11):
  19.  
  20. //cc prog.c -o prog -L/usr/X11R6/lib -lX11
  21.  
  22. // On SunOs 4 systems, the X libraries are placed in /usr/openwin/lib:
  23. // cc prog.c -o prog -L/usr/openwin/lib -lX11
  24.  
  25.  
  26.  
  27. #define GetRValue(dwColor) dwColor & 0x000000FF
  28. #define GetGValue(dwColor) (dwColor & 0x0000FF00) >> 8
  29. #define GetBValue(dwColor) dwColor >> 16
  30.  
  31.  
  32. int GetPixel(int x, int y)
  33. {
  34.     Display *dpy;
  35.     XImage *image;
  36.     XColor col;
  37.  
  38.     dpy = XOpenDisplay(NULL);
  39.     image = XGetImage(dpy, DefaultRootWindow(dpy),
  40.                       x, y, 1, 1, AllPlanes, ZPixmap);
  41.     col.pixel = XGetPixel(image, 0, 0);
  42.     XDestroyImage(image);
  43.  
  44.     // The XQueryColor() function returns the current RGB value for the pixel in the XColor structure and sets the DoRed, DoGreen, and DoBlue flags.
  45.     // XQueryColor() can generate BadColor and BadValue errors.
  46.     // BadColor A value for a Colormap argument does not name a defined Colormap.
  47.     // BadValue Some numeric value falls outside the range of values accepted by the request.
  48.     // Unless a specific range is specified for an argument, the full range defined by the argument's type
  49.     // is accepted. Any argument defined as a set of alternatives can generate this error.
  50.     XQueryColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), &col);
  51.     // The colormap is a lookup table located on the server the pixel value is used as an index into the table.
  52.  
  53.     XCloseDisplay(dpy);
  54.  
  55.     printf("R: %03d G: %03d B: %03d\n", (int)((double) col.red/257.0), (int)((double) col.green/257.0), (int)((double) col.blue/257.0));
  56.     return (col.red&0xff00)<<8 | (col.green)&0xff00 | (col.blue) >> 8; // = windows return value
  57. }
  58.  
  59.  
  60. int main()
  61. {
  62.     // this variable will contain the pointer to the Display structure
  63.     // returned when opening a connection.
  64.     Display* display;
  65.     int screen_num;
  66.     Screen *screen;
  67.     Window root_win;
  68.     XGCValues gc_val;
  69.  
  70.     // open the connection to the display "simey:0".
  71.     display = XOpenDisplay(0);// display = XOpenDisplay("simey:0");
  72.     if (display == NULL)
  73.     {
  74.         fprintf(stderr, "Cannot connect to X server %s\n", "simey:0");
  75.         exit (EXIT_FAILURE);
  76.     }
  77.  
  78.     XGrabServer(display);
  79.  
  80.     screen_num = DefaultScreen(display);
  81.     screen = XScreenOfDisplay(display, screen_num);
  82.     root_win = RootWindow(display, XScreenNumberOfScreen(screen));
  83.  
  84.     // Create a GC (Graphics Context) for the line
  85.     gc_val.function           = GXclear;
  86.     gc_val.plane_mask         = AllPlanes;
  87.     gc_val.foreground         = WhitePixel(display, screen_num);
  88.     gc_val.background         = BlackPixel(display, screen_num);
  89.     gc_val.line_width         = 4;
  90.     gc_val.line_style         = LineSolid;
  91.     gc_val.cap_style          = CapButt;
  92.     gc_val.join_style         = JoinMiter;
  93.     gc_val.fill_style         = FillOpaqueStippled;
  94.     gc_val.fill_rule          = WindingRule;
  95.     gc_val.graphics_exposures = False;
  96.     gc_val.clip_x_origin      = 0;
  97.     gc_val.clip_y_origin      = 0;
  98.     gc_val.clip_mask          = None;
  99.     gc_val.subwindow_mode     = IncludeInferiors;
  100.  
  101.     GC  gc_line = XCreateGC(display, root_win, GCFunction | GCPlaneMask |  GCForeground | GCBackground | GCLineWidth | GCLineStyle |
  102.                             GCCapStyle  | GCJoinStyle  |  GCFillStyle  |  GCFillRule  |  GCGraphicsExposures |
  103.                             GCClipXOrigin |  GCClipYOrigin  |  GCClipMask  | GCSubwindowMode, &gc_val);
  104.  
  105.     while( 1 )
  106.     {
  107.         // XSetForeground(display, gc_line, some_color);
  108.         // XDrawRectangle(display, root_win, gc_line, 50, 50, 400, 400);
  109.         for (int ix = 0; ix < 500;++ix)
  110.         {
  111.             for (int iy = 0; iy < 500;++iy)
  112.             {
  113.                 XDrawPoint(display, root_win, gc_line, ix, iy); // SetPixel
  114.             }
  115.         }
  116.  
  117.         usleep(100);
  118.     }
  119.  
  120.     XFlush(display);
  121.  
  122.     XUngrabServer(display);
  123.  
  124.  
  125.     XCloseDisplay( display );
  126.  
  127.     int iMyColor =  GetPixel(20,20) ;
  128.     printf("Sole color: 0x%08X\n", iMyColor);
  129.     printf("R: %03d G: %03d B: %03d\n", GetRValue(iMyColor), GetGValue(iMyColor), GetBValue(iMyColor));
  130.  
  131.     // printf("Sizof short: %ld\n", sizeof(unsigned short)) ; // 2 Bytes
  132.     return EXIT_SUCCESS ;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement