SHARE
TWEET

Untitled

a guest Dec 25th, 2016 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5.  
  6. #include <X11/Xlib.h>
  7.  
  8.  
  9. //-------------------------------------------------------------------------------------------------
  10. //  gcc -shared -O3 -Wall -fPIC -Wl,-soname,get_screen -o get_screen.so get_screen.c -lX11
  11. //-------------------------------------------------------------------------------------------------
  12.  
  13. GC createGC(Display* display, Window win){
  14.   GC gc;                              
  15.   int screen_num = DefaultScreen(display);
  16.   unsigned long valuemask = 0;        
  17.                                      
  18.   XGCValues values;                  
  19.  
  20.   values.subwindow_mode = IncludeInferiors;
  21.   values.line_width = 2;
  22.   valuemask = GCSubwindowMode | GCLineWidth;
  23.  
  24.   gc = XCreateGC(display, win, valuemask, &values);
  25.   if (gc < 0) {
  26.     fprintf(stderr, "XCreateGC: \n");
  27.   }
  28.  
  29.   XSetForeground(display, gc, BlackPixel(display, screen_num));
  30.   XSetBackground(display, gc, WhitePixel(display, screen_num));
  31.   return gc;
  32. }
  33.  
  34. void get_screen(const int dispNumber, const int xx,const int yy,const int W, const int H, /*out*/ unsigned char * data){
  35.   Display *display;
  36.   if (dispNumber == 1) {
  37.     display = XOpenDisplay(":1.0");
  38.     printf("Screenshoting display :1.0\n");
  39.   }else{
  40.     display = XOpenDisplay(":0.0");
  41.     printf("Screenshoting display :0.0\n");
  42.   }
  43.  
  44.   Window root = DefaultRootWindow(display);
  45.   XImage *image = XGetImage(display,root, xx,yy, W,H, AllPlanes, ZPixmap);
  46.  
  47.   unsigned long red_mask   = image->red_mask;
  48.   unsigned long green_mask = image->green_mask;
  49.   unsigned long blue_mask  = image->blue_mask;
  50.   int x, y;
  51.   int ii = 0;
  52.   for (y = 0; y < H; y++) {
  53.     for (x = 0; x < W; x++) {
  54.       unsigned long pixel = XGetPixel(image,x,y);
  55.       unsigned char blue  = (pixel & blue_mask);
  56.       unsigned char green = (pixel & green_mask) >> 8;
  57.       unsigned char red   = (pixel & red_mask) >> 16;
  58.  
  59.       data[ii + 2] = blue;
  60.       data[ii + 1] = green;
  61.       data[ii + 0] = red;
  62.       ii += 3;
  63.     }
  64.   }
  65.   XDestroyImage(image);
  66.   XDestroyWindow(display, root);
  67.   XCloseDisplay(display);
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top