`XSetWMNormalHints` and `XSetWMSizeHints`

1.3k views Asked by At

I'm confused with XSetWMNormalHints and XSetWMSizeHints. I want to set my window non-resizable, and a call to XSetWMNormalHints does it properly. But if I call XSetWMSizeHints instead, nothing really happens; the window is still resizable. How are the 2 functions used for my purpose, and what exactly does XSetWMSizeHints do? I've read the documentation multiple times, but I'm still confused, so asking a question here.

sh = XAllocSizeHints();
sh->flags = PMinSize | PMaxSize;
sh->min_width = sh->max_width = 100;
sh->min_height = sh->max_height = 100;
XSetWMNormalHints(d, w, sh);
//XSetWMSizeHints(d, w, sh, PMinSize | PMaxSize);
XFree(sh);
2

There are 2 answers

1
Thomas Dickey On

According to the manual page:

The XSetWMNormalHints function replaces the size hints for the WM_NORMAL_HINTS property on the specified window. If the property does not already exist, XSetWMNormalHints sets the size hints for the WM_NORMAL_HINTS property on the specified window. The property is stored with a type of WM_SIZE_HINTS and a format of 32.

while

The XSetWMSizeHints function replaces the size hints for the specified property on the named window. If the specified property does not already exist, XSetWMSizeHints sets the size hints for the specified property on the named window. The property is stored with a type of WM_SIZE_HINTS and a format of 32. To set a window's normal size hints, you can use the XSetWMNormalHints function.

and at the beginning, it shows

void XSetWMSizeHints(Display *display, Window w, XSizeHints *hints, Atom property); 

In your commented-out fragment, it does not use a property (does not appear to work). X11 Atoms (see Xlib Programming Manual: Properties and Atoms) are identifiers like a C pointer (but not actually an address), while PMinSize | PMaxSize is something entirely different: a value with specific bits set. The compiler accepted it because both are unsigned values, but at runtime that value probably does not match any Atom identifier.

XSetWMNormalHints is the more useful of the functions because it updates the property which you normally would use: WM_NORMAL_HINTS. The XSetWMSizeHints is more general — allowing you to use any property — but you probably do not need the generality.

1
Daniel LB On

This question is almost 4 years old, but perhaps this will clarify for others with similar questions.

Calling XSetWMSizeHints with a WM_NORMAL_HINTS property does the same thing as XSetWMNormalHints. The problem with the call to XSetWMSizeHints is that "PminSize | PMaxSize" is a value instead of a property. Here is the code modified to use XSetWMSizeHints with a property.

#include <X11/Xatom.h>  // XA_WM_NORMAL_HINTS
...
sh = XAllocSizeHints();
sh->flags = PMinSize | PMaxSize;
sh->min_width = sh->max_width = 100;
sh->min_height = sh->max_height = 100;
//XSetWMNormalHints(d, w, sh);
XSetWMSizeHints(d, w, sh, XA_WM_NORMAL_HINTS);
XFree(sh);