X11: Move to XSetWMProperties and add support to _NET_WM_PID
authorMarco Trevisan (TreviƱo)
Mon, 27 Jun 2011 19:30:52 +0200
changeset 6167c071e1372341
parent 6166 0bc1f6da9c74
child 6168 47dc1acea66f
X11: Move to XSetWMProperties and add support to _NET_WM_PID

Use the convenience function XSetWMProperties to set size, input
and class hints, it also automatically sets the WM_CLIENT_MACHINE
(this one needed by _NET_WM_PID) and WM_LOCALE_NAME windows hints.

Plus we add support to the _NET_WM_PID atom which is needed by many
windows managers to correctly associate a SDL window to its process
and to related .desktop file and icon for the given host.
src/video/x11/SDL_x11video.c
src/video/x11/SDL_x11video.h
src/video/x11/SDL_x11window.c
     1.1 --- a/src/video/x11/SDL_x11video.c	Sat Jan 07 01:28:06 2012 -0500
     1.2 +++ b/src/video/x11/SDL_x11video.c	Mon Jun 27 19:30:52 2011 +0200
     1.3 @@ -328,6 +328,9 @@
     1.4      /* Get the window class name, usually the name of the application */
     1.5      data->classname = get_classname();
     1.6  
     1.7 +    /* Get the process PID to be associated to the window */
     1.8 +    data->pid = getpid();
     1.9 +
    1.10      /* Open a connection to the X input manager */
    1.11  #ifdef X_HAVE_UTF8_STRING
    1.12      if (SDL_X11_HAVE_UTF8) {
     2.1 --- a/src/video/x11/SDL_x11video.h	Sat Jan 07 01:28:06 2012 -0500
     2.2 +++ b/src/video/x11/SDL_x11video.h	Mon Jun 27 19:30:52 2011 +0200
     2.3 @@ -69,6 +69,7 @@
     2.4  {
     2.5      Display *display;
     2.6      char *classname;
     2.7 +    pid_t pid;
     2.8      XIM im;
     2.9      Uint32 screensaver_activity;
    2.10      int numwindows;
     3.1 --- a/src/video/x11/SDL_x11window.c	Sat Jan 07 01:28:06 2012 -0500
     3.2 +++ b/src/video/x11/SDL_x11window.c	Mon Jun 27 19:30:52 2011 +0200
     3.3 @@ -258,11 +258,12 @@
     3.4      int depth;
     3.5      XSetWindowAttributes xattr;
     3.6      Window w;
     3.7 -    XSizeHints *sizehints;
     3.8 -    XWMHints *wmhints;
     3.9 -    XClassHint *classhints;
    3.10 +    XSizeHints sizehints;
    3.11 +    XWMHints wmhints;
    3.12 +    XClassHint classhints;
    3.13      Atom _NET_WM_WINDOW_TYPE;
    3.14      Atom _NET_WM_WINDOW_TYPE_NORMAL;
    3.15 +    Atom _NET_WM_PID;
    3.16      int wmstate_count;
    3.17      Atom wmstate_atoms[3];
    3.18      Uint32 fevent = 0;
    3.19 @@ -414,20 +415,6 @@
    3.20      }
    3.21  #endif
    3.22  
    3.23 -    sizehints = XAllocSizeHints();
    3.24 -    if (sizehints) {
    3.25 -        if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
    3.26 -            sizehints->min_width = sizehints->max_width = window->w;
    3.27 -            sizehints->min_height = sizehints->max_height = window->h;
    3.28 -            sizehints->flags = PMaxSize | PMinSize;
    3.29 -        }
    3.30 -        sizehints->x = window->x;
    3.31 -        sizehints->y = window->y;
    3.32 -        sizehints->flags |= USPosition;
    3.33 -        XSetWMNormalHints(display, w, sizehints);
    3.34 -        XFree(sizehints);
    3.35 -    }
    3.36 -
    3.37      if (window->flags & SDL_WINDOW_BORDERLESS) {
    3.38          SDL_bool set;
    3.39          Atom WM_HINTS;
    3.40 @@ -511,22 +498,32 @@
    3.41          }
    3.42      }
    3.43  
    3.44 -    /* Set the input hints so we get keyboard input */
    3.45 -    wmhints = XAllocWMHints();
    3.46 -    if (wmhints) {
    3.47 -        wmhints->input = True;
    3.48 -        wmhints->flags = InputHint;
    3.49 -        XSetWMHints(display, w, wmhints);
    3.50 -        XFree(wmhints);
    3.51 +    /* Setup the normal size hints */
    3.52 +    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
    3.53 +        sizehints.min_width = sizehints.max_width = window->w;
    3.54 +        sizehints.min_height = sizehints.max_height = window->h;
    3.55 +        sizehints.flags = PMaxSize | PMinSize;
    3.56      }
    3.57 +    sizehints.x = window->x;
    3.58 +    sizehints.y = window->y;
    3.59 +    sizehints.flags |= USPosition;
    3.60  
    3.61 -    /* Set the class hints so we can get an icon (AfterStep) */
    3.62 -    classhints = XAllocClassHint();
    3.63 -    if (classhints != NULL) {
    3.64 -        classhints->res_name = data->classname;
    3.65 -        classhints->res_class = data->classname;
    3.66 -        XSetClassHint(display, w, classhints);
    3.67 -        XFree(classhints);
    3.68 +    /* Setup the input hints so we get keyboard input */
    3.69 +    wmhints.input = True;
    3.70 +    wmhints.flags = InputHint;
    3.71 +
    3.72 +    /* Setup the class hints so we can get an icon (AfterStep) */
    3.73 +    classhints.res_name = data->classname;
    3.74 +    classhints.res_class = data->classname;
    3.75 +
    3.76 +    /* Set the size, input and class hints, and define WM_CLIENT_MACHINE and WM_LOCALE_NAME */
    3.77 +    XSetWMProperties(display, w, NULL, NULL, NULL, 0, &sizehints, &wmhints, &classhints);
    3.78 +
    3.79 +    /* Set the PID related to the window for the given hostname, if possible */
    3.80 +    if (data->pid > 0) {
    3.81 +        _NET_WM_PID = XInternAtom(display, "_NET_WM_PID", False);
    3.82 +        XChangeProperty(display, w, _NET_WM_PID, XA_CARDINAL, 32, PropModeReplace,
    3.83 +                        (unsigned char *)&data->pid, 1);
    3.84      }
    3.85  
    3.86      /* Set the window manager state */