Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <X11/Xlib.h>
  3. #include <X11/Xutil.h>
  4.  
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <X11/extensions/XShm.h>
  8.  
  9. void slop(Window *window, unsigned int *w, unsigned int *h)
  10. {
  11. FILE *slop;
  12. char out[256];
  13.  
  14. slop = popen("/usr/bin/slop -f '%i,%w,%h'", "r");
  15. fread(out, sizeof(out), 1, slop);
  16. pclose(slop);
  17.  
  18. sscanf(out, "%lu,%u,%u", window, w, h);
  19. }
  20.  
  21. int main(int argc, char **argv)
  22. {
  23. Display *display;
  24. Window window_in, window_out;
  25. GC gc;
  26. XEvent ev;
  27. XWindowAttributes attrs;
  28. XShmSegmentInfo shminfo;
  29. XImage *image;
  30. unsigned int w,h;
  31.  
  32. slop(&window_in, &w, &h);
  33.  
  34. display = XOpenDisplay(NULL);
  35. gc = DefaultGC(display, DefaultScreen(display));
  36.  
  37. window_out = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, w, h, 0, 0, 0);
  38. XSelectInput(display, window_out, StructureNotifyMask | ButtonPressMask);
  39. XMapWindow(display, window_out);
  40.  
  41. XNextEvent(display, &ev); // wait for window to be mapped
  42.  
  43. do {
  44. XGrabServer(display);
  45. XGetWindowAttributes(display, window_in, &attrs);
  46.  
  47. // doesn't have the bug
  48. //image = XGetImage(display, window_in, 0, 0, attrs.width, attrs.height, 0xffffffff, ZPixmap);
  49.  
  50. // has the bug
  51. image = XShmCreateImage(display, attrs.visual, attrs.depth, ZPixmap, NULL, &shminfo, attrs.width, attrs.height);
  52. shminfo.shmid = shmget(IPC_PRIVATE, image->bytes_per_line * image->height, IPC_CREAT|0777);
  53. shminfo.shmaddr = image->data = shmat(shminfo.shmid, 0, 0);
  54. shminfo.readOnly = False;
  55. XShmAttach(display, &shminfo);
  56. XShmGetImage(display, window_in, image, 0, 0, AllPlanes);
  57.  
  58. XUngrabServer(display);
  59. XPutImage(display, window_out, gc, image, 0, 0, 0, 0, attrs.width, attrs.height);
  60.  
  61. XShmDetach(display, &shminfo);
  62. XDestroyImage(image);
  63. shmdt(shminfo.shmaddr);
  64. shmctl(shminfo.shmid, IPC_RMID, 0);
  65.  
  66. XNextEvent(display, &ev);
  67. } while (ev.type != ButtonPress);
  68.  
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement