Guest User

Untitled

a guest
Jun 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. ## build.sh
  2. gcc -lX11 windowlist.c -o windowlist -L/usr/X11R6/lib -I/usr/X11R6/include
  3.  
  4. ## widowlist.c
  5. #include <X11/Xlib.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. Display *display;
  11. Window window;
  12. XEvent event;
  13. int screen;
  14.  
  15. Atom type;
  16. int format;
  17. unsigned char **clientlist, *titletext;
  18. unsigned long nitems, bytes;
  19. int clientcount, activewin;
  20.  
  21. void *getproperty (Window win, Atom at, Atom type, int *num_results) {
  22.  
  23. Atom type_ret;
  24. int format = 0;
  25. unsigned long items = 0;
  26. unsigned long bytes = 0;
  27. unsigned char **prop_value;
  28. int result;
  29.  
  30. result = XGetWindowProperty(display, win, at, 0, 0x7fffffff, False, type, &type_ret, &format, &nitems, &bytes, (unsigned char **)&prop_value);
  31.  
  32. if (result != 0) {
  33. printf ("result from XGetWindowProperty not 0, %d instead!\n", result);
  34. }
  35.  
  36. *num_results = nitems;
  37.  
  38. return prop_value;
  39.  
  40. }
  41.  
  42. char *getwindowname(Window window) {
  43.  
  44. char *windowname;
  45. int items;
  46.  
  47. windowname = getproperty(window, XInternAtom(display, "WM_ICON_NAME", True), AnyPropertyType, &items);
  48.  
  49. return (char *)(windowname ? windowname : "Unnamed Window");
  50.  
  51. }
  52.  
  53. unsigned char **getwindowlist(int *clientcount) {
  54.  
  55. int i;
  56.  
  57. clientlist = getproperty(RootWindow(display, 0), XInternAtom(display, "_NET_CLIENT_LIST", True), AnyPropertyType, clientcount);
  58.  
  59. return clientlist;
  60.  
  61. }
  62.  
  63. int main(void) {
  64.  
  65. int i = 0;
  66.  
  67. if( !(display = XOpenDisplay(NULL)) ){
  68. fprintf(stderr, "Cannot open display\n");
  69. exit(1);
  70. }
  71.  
  72. clientlist = getwindowlist((int *)&clientcount);
  73.  
  74. for (i = 0; i < clientcount; i++) {
  75.  
  76. XSelectInput (display, (int)clientlist[i], StructureNotifyMask | FocusChangeMask | VisibilityChangeMask);
  77.  
  78. printf("%u: %s\n", clientlist[i], getwindowname((int)clientlist[i]));
  79.  
  80. }
  81.  
  82. }
Add Comment
Please, Sign In to add comment