Advertisement
Guest User

mbt

a guest
Jul 20th, 2009
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. /*
  2.  * intercept-wm-close.c - WM_DELETE_WINDOW experiment, part 2
  3.  * Copyright (C) 2009 Michael B. Trausch <[email protected]>
  4.  *
  5.  * Determines whether or not we can intercept an WM_DELETE_WINDOW
  6.  * event, such that we would be able to direct a window back to the
  7.  * system tray instead of actually delete it.
  8.  */
  9.  
  10. // Standard UNIX includes
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. // GNU readline
  16. #include <readline/readline.h>
  17. #include <readline/history.h>
  18.  
  19. // Xlib
  20. #include <X11/Xlib.h>
  21. #include <X11/Xatom.h>
  22.  
  23. // glib and gdk
  24. #include <glib.h>
  25. #include <gdk/gdk.h>
  26.  
  27. #define RETVAL_SUCCESS 0
  28. #define RETVAL_NO_X11_DISPLAY 1
  29. #define RETVAL_GDK_FAILED 2
  30.  
  31. GdkFilterReturn
  32. cb(GdkXEvent *xev, GdkEvent *ev, gpointer data) {
  33.   XEvent *real_xev = (XEvent *)xev;
  34.   if(real_xev->type == ClientMessage) {
  35.     fprintf(stdout, "Got an event.\n");
  36.     fprintf(stdout, "  ... for window %ld.\n", real_xev->xclient.window);
  37.   }
  38.  
  39.   return(GDK_FILTER_CONTINUE);
  40. }
  41.  
  42. int
  43. main(int argc, char *argv[]) {
  44.   GdkDisplay *gd = NULL;
  45.   GdkAtom wm_delete_window;
  46.   int found = 0;
  47.   Window w = (Window)0;
  48.   char *line = NULL;
  49.   gboolean gdkis = FALSE;
  50.   GMainLoop *gml = NULL;
  51.  
  52.   gml = g_main_loop_new(NULL, FALSE);
  53.  
  54.   gdkis = gdk_init_check(&argc, &argv);
  55.   if(gdkis == FALSE) {
  56.     fprintf(stderr, "Cannot open display, exiting.\n");
  57.     exit(RETVAL_NO_X11_DISPLAY);
  58.   }
  59.  
  60.   gd = gdk_display_open(NULL);
  61.  
  62.   wm_delete_window = gdk_atom_intern("WM_DELETE_WINDOW", FALSE);
  63.  
  64.   while(!found) {
  65.   try_again:
  66.     line = readline("Enter the X11 ID to intercept WM_DELETE_WINDOW for: ");
  67.  
  68.     if(line == NULL) {
  69.       fprintf(stderr, "Error: invalid window ID\n");
  70.       goto try_again;
  71.     }
  72.  
  73.     w = (Window)atol(line);
  74.  
  75.     GdkWindow *gw = gdk_window_foreign_new((GdkNativeWindow)w);
  76.     if(gw != NULL) {
  77.       found = 1;
  78.     } else {
  79.       fprintf(stderr, "Error: window ID %ld did not return a result.\n", w);
  80.     }
  81.   }
  82.  
  83.   fprintf(stdout, "Broke out of the loop, found the window.\n");
  84.   gdk_display_add_client_message_filter(gd, wm_delete_window, cb, NULL);
  85.   gdk_window_add_filter(gw, cb, NULL);
  86.  
  87.   g_main_loop_run(gml);
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement