I want to judge whether the specific window is minimized. But the coordinate is the same as not minimized. How to determine whether the window is minimized? I find the question which had been marked solved, but it doesn't work.
Asked
Active
Viewed 637 times
2
-
"But it doesn't work" is not a very useful description. What have you tried and what is the result? Please edit your question to include a [MCVE](https://stackoverflow.com/help/mcve). Regarding the linked question, what value did you read when you tried the solution from the accepted answer? – Gerhardh Jul 27 '21 at 09:06
-
@Gerhardh Thanks for your reply. When XGetWindowProperty has been called, num_items is zero. There is a difference is vdl_x11_usefull_atoms->_NET_WM_STATE, I use WM_STATE because I don't find the declaration of vdl_x11_usefull_atoms. Do you have any suggestion more? Thank you! – Arthurian Jul 27 '21 at 09:30
-
There is no 'minimized' state. The window is either shown on screen or hidden (iconified). If it is hidden, why should the coordinates field be modified? What if you want to restore it on the screen? Unnecessary copy of the coordinates. The minimization is just a visual effect of the window manager, that shows the user where the window has gone and that it is restorable from there. – Erdal Küçük Jul 27 '21 at 10:59
1 Answers
3
Based on your comment above, try this:
//try to get the atoms, don't create it if it does not exist
Atom wm_state = XInternAtom(dpy, "_NET_WM_STATE", True);
Atom wm_hidden = XInternAtom(dpy, "_NET_WM_STATE_HIDDEN", True);
//test if the atoms do exists
if (wm_state == None) { /*does not exist*/ }
if (wm_hidden == None) { /*does not exist*/ }
Then call the XGetWindowProperty
function:
long max_length = 1024;
Atom actual_type;
int actual_format;
unsigned long bytes_after, num_states = 0;
Atom* states = NULL;
if (XGetWindowProperty(
dpy, //your display handle
id, //your windows handle
wm_state, //the atom you received earlier
0l, //no offset
max_length,
False, //do not delete
XA_ATOM, //requested type
&actual_type, //atom identifier that defines the actual type
&actual_format, //actual format of the property
&num_states, //actual number of items stored in the states data
&bytes_after, //number of bytes remaining on a partial read
(unsigned char**) &states //data in the specified format
) == Success) {
//iterate over the returned list of atoms
for (unsigned long i = 0; i < num_states; ++i) {
//test if the list contains the 'hidden' state
if (states[i] == wm_hidden) { /* state is set */ }
}
}
Reference:
Example (based on the discussion):
/* window states */
typedef enum {
WINDOW_STATE_NONE = 0,
WINDOW_STATE_MODAL = (1 << 0),
WINDOW_STATE_STICKY = (1 << 1),
WINDOW_STATE_MAXIMIZED_VERT = (1 << 2),
WINDOW_STATE_MAXIMIZED_HORZ = (1 << 3),
WINDOW_STATE_MAXIMIZED = (WINDOW_STATE_MAXIMIZED_VERT | WINDOW_STATE_MAXIMIZED_HORZ),
WINDOW_STATE_SHADED = (1 << 4),
WINDOW_STATE_SKIP_TASKBAR = (1 << 5),
WINDOW_STATE_SKIP_PAGER = (1 << 6),
WINDOW_STATE_HIDDEN = (1 << 7),
WINDOW_STATE_FULLSCREEN = (1 << 8),
WINDOW_STATE_ABOVE = (1 << 9),
WINDOW_STATE_BELOW = (1 << 10),
WINDOW_STATE_DEMANDS_ATTENTION = (1 << 11),
WINDOW_STATE_FOCUSED = (1 << 12),
WINDOW_STATE_SIZE = 13,
} window_state_t;
/* state names */
static char* WINDOW_STATE_NAMES[] = {
"_NET_WM_STATE_MODAL",
"_NET_WM_STATE_STICKY",
"_NET_WM_STATE_MAXIMIZED_VERT",
"_NET_WM_STATE_MAXIMIZED_HORZ",
"_NET_WM_STATE_SHADED",
"_NET_WM_STATE_SKIP_TASKBAR",
"_NET_WM_STATE_SKIP_PAGER",
"_NET_WM_STATE_HIDDEN",
"_NET_WM_STATE_FULLSCREEN",
"_NET_WM_STATE_ABOVE",
"_NET_WM_STATE_BELOW",
"_NET_WM_STATE_DEMANDS_ATTENTION",
"_NET_WM_STATE_FOCUSED"
};
/* some window struct */
typedef struct {
Display *dpy;
Window id;
struct {
Atom NET_WM_STATE;
Atom NET_WM_STATES[WINDOW_STATE_SIZE];
} atoms;
} window_t;
window_t win;
/* in window initialization function */
win->atoms.NET_WM_STATE = XInternAtom(win->dpy, "_NET_WM_STATE", False);
for (i=0; i < WINDOW_STATE_SIZE; ++i) {
win->atoms.NET_WM_STATES[i] = XInternAtom(win->dpy, WINDOW_STATE_NAMES[i], False);
}
/* a function to retrieve the current state of the window */
window_state_t get_window_state(window_t *win)
{
long max_length = 1024;
Atom actual_type;
int actual_format;
unsigned long bytes_after, i, num_states = 0;
Atom* states = NULL;
window_state_t state = WINDOW_STATE_NONE;
if (XGetWindowProperty(win->dpy,
win->id,
win->atoms.NET_WM_STATE,
0l,
max_length,
False,
XA_ATOM,
&actual_type,
&actual_format,
&num_states,
&bytes_after,
(unsigned char**) &states) == Success)
{
//for every state we get from the server
for (i = 0; i < num_states; ++i) {
//for every (known) state
for (int n=0; n < WINDOW_STATE_SIZE; ++n) {
//test the state at index i
if (states[i] == win->atoms.NET_WM_STATES[n]) {
state |= (1 << n);
break;
}
}
}
XFree(states);
}
return state;
}

Erdal Küçük
- 4,810
- 1
- 6
- 11
-
-
Call `XInternAtom` with `False` instead `True`. I looked into my (old) code and i also used `False`. I apologize for the confusion, maybe the hints are only set, if they exist, i don't know. – Erdal Küçük Jul 27 '21 at 12:00
-
Thank you very much. I have solved the problem. Maybe "if (states[i] == wm_hidden)" is incorrect, I find wm_hidden is 601 not equals with state[i]. I use "if (0x2 & states[i])" and it can satisfied my requirement. I don't know whether it is correct. – Arthurian Jul 27 '21 at 13:09
-
After a call to `XGetWindowProperty` the `states` list will contain the `wm_hidden` value, if the state was set, otherwise not. How else will you distinguish which property was set or not, if not by their names? Do you really think that `0x2 & states[i]` looks somehow correct? Let us see your code, maybe we can together find out, what went wrong. – Erdal Küçük Jul 27 '21 at 13:23
-
‘ if(Success == XGetWindowProperty( display, wnd, wm_state, 0l, max_length, False, XA_ATOM, &actual_type, &actual_format, &num_states, &bytes_after, (unsigned char**)&states) ) { qDebug("num_states: %d, wm_hidden : %d", num_states, wm_hidden); for(unsigned long i = 0; i < num_states; ++i) { qDebug("states[%d]: %d", i, states[i]); if(wm_hidden == states[i]) { qDebug("i: %d", i); } if(0x2 & states[i]) { ;// do something } } } ’ – Arthurian Jul 28 '21 at 01:49
-
-
states[0]: 0x1c8 while the windows is hidden, 0x1ca while the window is show and 0x14e When the process is first opened – Arthurian Jul 28 '21 at 02:01
-
_NET_WM_STATE: "A **list** of hints describing the window state." That the content of the list will be different after a **state change** of the window, shouldn't be a surprise. The real question is, which number belongs to which symbol (Atom). You mentioned before, that `wm_hidden` never occurs in the returned list, does it or not? Best thing to do is, to print the list by their names after changing the state of the window, like `if (states[i] == wm_xxx) printf("wm_xxx\n");`. – Erdal Küçük Jul 28 '21 at 09:10
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235377/discussion-between-arthurian-and-erdal-kucuk). – Arthurian Jul 28 '21 at 09:50
-
`Atom wm_hidden = XInternAtom(dpy, "_NET_WM_HIDDEN", True);` should be `Atom wm_hidden = XInternAtom(dpy, "_NET_WM_STATE_HIDDEN", True);`. Isn't it? – Sergey Anisimov Apr 10 '23 at 00:31