Partial fix for focus stealing
[wmaker-crm.git] / src / wmspec.c
blob95985dba763f5ac026f7f8ca4f140cef901fa3f5
1 /* wmspec.c-- support for the wm-spec Hints
3 * Window Maker window manager
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * TODO
24 * ----
26 * This file needs to be checked for all calls to XGetWindowProperty() and
27 * proper checks need to be made on the returned values. Only checking for
28 * return to be Success is not enough. -Dan
31 #include "wconfig.h"
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
35 #include <X11/Xarch.h>
36 #include <string.h>
38 #include <WINGs/WUtil.h>
39 #include "WindowMaker.h"
40 #include "window.h"
41 #include "screen.h"
42 #include "workspace.h"
43 #include "framewin.h"
44 #include "actions.h"
45 #include "client.h"
46 #include "appicon.h"
47 #include "wmspec.h"
48 #include "icon.h"
49 #include "stacking.h"
50 #include "xinerama.h"
51 #include "properties.h"
53 /* Global variables */
54 extern Atom _XA_WM_DELETE_WINDOW;
55 extern Time LastTimestamp;
56 extern WPreferences wPreferences;
58 /* Root Window Properties */
59 static Atom net_supported;
60 static Atom net_client_list;
61 static Atom net_client_list_stacking;
62 static Atom net_number_of_desktops;
63 static Atom net_desktop_geometry;
64 static Atom net_desktop_viewport;
65 static Atom net_current_desktop;
66 static Atom net_desktop_names;
67 static Atom net_active_window;
68 static Atom net_supporting_wm_check;
69 static Atom net_virtual_roots; /* N/A */
70 static Atom net_desktop_layout; /* XXX */
71 static Atom net_showing_desktop;
73 /* Other Root Window Messages */
74 static Atom net_close_window;
75 static Atom net_moveresize_window; /* TODO */
76 static Atom net_wm_moveresize; /* TODO */
78 /* Application Window Properties */
79 static Atom net_wm_name;
80 static Atom net_wm_visible_name; /* TODO (unnecessary?) */
81 static Atom net_wm_icon_name;
82 static Atom net_wm_visible_icon_name; /* TODO (unnecessary?) */
83 static Atom net_wm_desktop;
84 static Atom net_wm_window_type;
85 static Atom net_wm_window_type_desktop;
86 static Atom net_wm_window_type_dock;
87 static Atom net_wm_window_type_toolbar;
88 static Atom net_wm_window_type_menu;
89 static Atom net_wm_window_type_utility;
90 static Atom net_wm_window_type_splash;
91 static Atom net_wm_window_type_dialog;
92 static Atom net_wm_window_type_normal;
93 static Atom net_wm_state;
94 static Atom net_wm_state_modal; /* XXX: what is this?!? */
95 static Atom net_wm_state_sticky;
96 static Atom net_wm_state_maximized_vert;
97 static Atom net_wm_state_maximized_horz;
98 static Atom net_wm_state_shaded;
99 static Atom net_wm_state_skip_taskbar;
100 static Atom net_wm_state_skip_pager;
101 static Atom net_wm_state_hidden;
102 static Atom net_wm_state_fullscreen;
103 static Atom net_wm_state_above;
104 static Atom net_wm_state_below;
105 static Atom net_wm_allowed_actions;
106 static Atom net_wm_action_move;
107 static Atom net_wm_action_resize;
108 static Atom net_wm_action_minimize;
109 static Atom net_wm_action_shade;
110 static Atom net_wm_action_stick;
111 static Atom net_wm_action_maximize_horz;
112 static Atom net_wm_action_maximize_vert;
113 static Atom net_wm_action_fullscreen;
114 static Atom net_wm_action_change_desktop;
115 static Atom net_wm_action_close;
116 static Atom net_wm_strut; /* XXX: see net_workarea */
117 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
118 static Atom net_wm_icon_geometry; /* FIXME: should work together with net_wm_handled_icons, gnome-panel-2.2.0.1 doesn't use _NET_WM_HANDLED_ICONS, thus present situation. */
119 static Atom net_wm_icon;
120 static Atom net_wm_pid; /* TODO */
121 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
123 /* Window Manager Protocols */
124 static Atom net_wm_ping; /* TODO */
126 static Atom utf8_string;
128 typedef struct {
129 char *name;
130 Atom *atom;
131 } atomitem_t;
133 static atomitem_t atomNames[] = {
134 {"_NET_SUPPORTED", &net_supported},
135 {"_NET_CLIENT_LIST", &net_client_list},
136 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
137 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
138 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
139 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
140 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
141 {"_NET_DESKTOP_NAMES", &net_desktop_names},
142 {"_NET_ACTIVE_WINDOW", &net_active_window},
143 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
144 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
145 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
146 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
148 {"_NET_CLOSE_WINDOW", &net_close_window},
149 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
150 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
152 {"_NET_WM_NAME", &net_wm_name},
153 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
154 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
155 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
156 {"_NET_WM_DESKTOP", &net_wm_desktop},
157 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
158 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
159 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
160 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
161 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
162 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
163 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
164 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
165 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
166 {"_NET_WM_STATE", &net_wm_state},
167 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
168 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
169 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
170 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
171 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
172 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
173 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
174 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
175 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
176 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
177 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
178 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
179 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
180 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
181 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
182 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
183 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
184 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
185 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
186 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
187 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
188 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
189 {"_NET_WM_STRUT", &net_wm_strut},
190 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
191 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
192 {"_NET_WM_ICON", &net_wm_icon},
193 {"_NET_WM_PID", &net_wm_pid},
194 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
196 {"_NET_WM_PING", &net_wm_ping},
198 {"UTF8_STRING", &utf8_string},
201 #define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
203 #define _NET_WM_STATE_REMOVE 0
204 #define _NET_WM_STATE_ADD 1
205 #define _NET_WM_STATE_TOGGLE 2
207 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
208 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
209 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
210 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
211 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
212 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
213 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
214 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
215 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
216 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
217 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
219 static void observer(void *self, WMNotification * notif);
220 static void wsobserver(void *self, WMNotification * notif);
222 static void updateClientList(WScreen * scr);
223 static void updateClientListStacking(WScreen * scr, WWindow *);
225 static void updateWorkspaceNames(WScreen * scr);
226 static void updateCurrentWorkspace(WScreen * scr);
227 static void updateWorkspaceCount(WScreen * scr);
229 typedef struct NetData {
230 WScreen *scr;
231 WReservedArea *strut;
232 WWindow **show_desktop;
233 } NetData;
235 static void setSupportedHints(WScreen * scr)
237 Atom atom[atomNr];
238 int i = 0;
240 /* set supported hints list */
241 /* XXX: extend this !!! */
243 atom[i++] = net_client_list;
244 atom[i++] = net_client_list_stacking;
245 atom[i++] = net_number_of_desktops;
246 atom[i++] = net_desktop_geometry;
247 atom[i++] = net_desktop_viewport;
248 atom[i++] = net_current_desktop;
249 atom[i++] = net_desktop_names;
250 atom[i++] = net_active_window;
251 atom[i++] = net_supporting_wm_check;
252 atom[i++] = net_showing_desktop;
253 #if 0
254 atom[i++] = net_wm_moveresize;
255 #endif
256 atom[i++] = net_wm_desktop;
257 atom[i++] = net_wm_window_type;
258 atom[i++] = net_wm_window_type_desktop;
259 atom[i++] = net_wm_window_type_dock;
260 atom[i++] = net_wm_window_type_toolbar;
261 atom[i++] = net_wm_window_type_menu;
262 atom[i++] = net_wm_window_type_utility;
263 atom[i++] = net_wm_window_type_splash;
264 atom[i++] = net_wm_window_type_dialog;
265 atom[i++] = net_wm_window_type_normal;
267 atom[i++] = net_wm_state;
268 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
269 atom[i++] = net_wm_state_sticky;
270 atom[i++] = net_wm_state_shaded;
271 atom[i++] = net_wm_state_maximized_horz;
272 atom[i++] = net_wm_state_maximized_vert;
273 atom[i++] = net_wm_state_skip_taskbar;
274 atom[i++] = net_wm_state_skip_pager;
275 atom[i++] = net_wm_state_hidden;
276 atom[i++] = net_wm_state_fullscreen;
277 atom[i++] = net_wm_state_above;
278 atom[i++] = net_wm_state_below;
280 atom[i++] = net_wm_allowed_actions;
281 atom[i++] = net_wm_action_move;
282 atom[i++] = net_wm_action_resize;
283 atom[i++] = net_wm_action_minimize;
284 atom[i++] = net_wm_action_shade;
285 atom[i++] = net_wm_action_stick;
286 atom[i++] = net_wm_action_maximize_horz;
287 atom[i++] = net_wm_action_maximize_vert;
288 atom[i++] = net_wm_action_fullscreen;
289 atom[i++] = net_wm_action_change_desktop;
290 atom[i++] = net_wm_action_close;
292 atom[i++] = net_wm_strut;
293 atom[i++] = net_wm_icon_geometry;
294 atom[i++] = net_wm_icon;
295 atom[i++] = net_wm_handled_icons;
297 atom[i++] = net_wm_name;
298 atom[i++] = net_wm_icon_name;
300 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
302 /* set supporting wm hint */
303 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
304 PropModeReplace, (unsigned char *)&scr->info_window, 1);
306 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
307 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
310 void wNETWMUpdateDesktop(WScreen * scr)
312 long *views, sizes[2];
313 int count, i;
315 if (scr->workspace_count == 0)
316 return;
318 count = scr->workspace_count * 2;
319 views = wmalloc(sizeof(long) * count);
320 /*memset(views, 0, sizeof(long) * count); */
321 sizes[0] = scr->scr_width;
322 sizes[1] = scr->scr_height;
324 for (i = 0; i < scr->workspace_count; i++) {
325 views[2 * i + 0] = 0;
326 views[2 * i + 1] = 0;
329 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
330 PropModeReplace, (unsigned char *)sizes, 2);
332 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
333 PropModeReplace, (unsigned char *)views, count);
335 wfree(views);
338 int wNETWMGetCurrentDesktopFromHint(WScreen * scr)
340 int count;
341 unsigned char *prop;
343 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
344 if (prop) {
345 int desktop = *(long *)prop;
346 XFree(prop);
347 return desktop;
349 return -1;
353 * Find the best icon to be used by Window Maker for appicon/miniwindows.
354 * Currently the algorithm is to take the image with the size closest
355 * to icon_size x icon_size, but never bigger than that.
357 * This algorithm is very poorly implemented and needs to be redone (it can
358 * easily select images with very large widths and very small heights over
359 * square images, if the area of the former is closer to the desired one).
361 * The logic can also be changed to accept bigger images and scale them down.
363 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
365 int size, wanted, d;
366 unsigned long i, distance;
367 unsigned long *icon;
369 /* better use only 75% of icon_size. For 64x64 this means 48x48
370 * This leaves room around the icon for the miniwindow title and
371 * results in better overall aesthetics -Dan */
372 wanted = wPreferences.icon_size * wPreferences.icon_size;
374 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
375 size = data[i] * data[i + 1];
376 if (size == 0)
377 break;
378 d = wanted - size;
379 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
380 distance = d;
381 icon = &data[i];
383 i += size + 2;
386 return icon;
389 static RImage *makeRImageFromARGBData(unsigned long *data)
391 int size, width, height, i;
392 RImage *image;
393 unsigned char *imgdata;
394 unsigned long pixel;
396 width = data[0];
397 height = data[1];
398 size = width * height;
400 if (size == 0)
401 return NULL;
403 image = RCreateImage(width, height, True);
405 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
406 pixel = data[i];
407 #if BYTE_ORDER == BIG_ENDIAN
408 imgdata[2] = (pixel >> 24) & 0xff; /* A */
409 imgdata[1] = (pixel >> 16) & 0xff; /* R */
410 imgdata[0] = (pixel >> 8) & 0xff; /* G */
411 imgdata[3] = (pixel >> 0) & 0xff; /* B */
412 #else /* Little endian */
413 imgdata[3] = (pixel >> 24) & 0xff; /* A */
414 imgdata[0] = (pixel >> 16) & 0xff; /* R */
415 imgdata[1] = (pixel >> 8) & 0xff; /* G */
416 imgdata[2] = (pixel >> 0) & 0xff; /* B */
417 #endif /* endianness */
421 return image;
424 static void updateIconImage(WWindow * wwin)
426 unsigned long *property, *data;
427 unsigned long items, rest;
428 Atom type;
429 int format;
431 if (wwin->net_icon_image)
432 RReleaseImage(wwin->net_icon_image);
433 wwin->net_icon_image = NULL;
435 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
436 False, XA_CARDINAL, &type, &format, &items, &rest,
437 (unsigned char **)&property) != Success || !property) {
438 return;
441 if (type != XA_CARDINAL || format != 32 || items < 2) {
442 XFree(property);
443 return;
446 data = findBestIcon(property, items);
447 if (!data) {
448 XFree(property);
449 return;
452 wwin->net_icon_image = makeRImageFromARGBData(data);
454 XFree(property);
456 if (wwin->icon) wIconUpdate(wwin->icon);
457 WApplication *app = wApplicationOf(wwin->main_window);
458 if (app && app->app_icon){
459 wIconUpdate(app->app_icon->icon);
460 wAppIconPaint(app->app_icon);
464 static void updateShowDesktop(WScreen * scr, Bool show)
466 long foo;
468 foo = (show == True);
469 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
470 PropModeReplace, (unsigned char *)&foo, 1);
473 void wNETWMShowingDesktop(WScreen * scr, Bool show)
475 if (show && scr->netdata->show_desktop == NULL) {
476 WWindow *tmp, **wins;
477 int i = 0;
479 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
481 tmp = scr->focused_window;
482 while (tmp) {
483 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
485 wins[i++] = tmp;
486 tmp->flags.skip_next_animation = 1;
487 tmp->flags.net_show_desktop = 1;
488 wIconifyWindow(tmp);
491 tmp = tmp->prev;
493 wins[i++] = NULL;
495 scr->netdata->show_desktop = wins;
496 updateShowDesktop(scr, True);
497 } else if (scr->netdata->show_desktop != NULL) {
498 /* FIXME: get rid of workspace flashing ! */
499 int ws = scr->current_workspace;
500 WWindow **tmp;
501 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
502 wDeiconifyWindow(*tmp);
503 (*tmp)->flags.net_show_desktop = 0;
505 if (ws != scr->current_workspace)
506 wWorkspaceChange(scr, ws);
507 wfree(scr->netdata->show_desktop);
508 scr->netdata->show_desktop = NULL;
509 updateShowDesktop(scr, False);
513 void wNETWMInitStuff(WScreen * scr)
515 NetData *data;
516 int i;
518 #ifdef DEBUG_WMSPEC
519 wmessage("enter");
520 #endif
522 #ifdef HAVE_XINTERNATOMS
524 Atom atoms[atomNr];
525 char *names[atomNr];
527 for (i = 0; i < atomNr; ++i) {
528 names[i] = atomNames[i].name;
530 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
531 for (i = 0; i < atomNr; ++i) {
532 *atomNames[i].atom = atoms[i];
535 #else
536 for (i = 0; i < atomNr; i++) {
537 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
539 #endif
541 data = wmalloc(sizeof(NetData));
542 data->scr = scr;
543 data->strut = NULL;
544 data->show_desktop = NULL;
546 scr->netdata = data;
548 setSupportedHints(scr);
550 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
551 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
552 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
553 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
554 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
555 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
556 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
558 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
559 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
560 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
561 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
563 updateClientList(scr);
564 updateClientListStacking(scr, NULL);
565 updateWorkspaceCount(scr);
566 updateWorkspaceNames(scr);
567 updateShowDesktop(scr, False);
569 wScreenUpdateUsableArea(scr);
572 void wNETWMCleanup(WScreen * scr)
574 int i;
576 for (i = 0; i < atomNr; i++)
577 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
580 void wNETWMUpdateActions(WWindow * wwin, Bool del)
582 Atom action[10]; /* nr of actions atoms defined */
583 int i = 0;
585 if (del) {
586 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
587 return;
590 if (IS_MOVABLE(wwin))
591 action[i++] = net_wm_action_move;
593 if (IS_RESIZABLE(wwin))
594 action[i++] = net_wm_action_resize;
596 if (!WFLAGP(wwin, no_miniaturizable))
597 action[i++] = net_wm_action_minimize;
599 if (!WFLAGP(wwin, no_shadeable))
600 action[i++] = net_wm_action_shade;
602 /* if (!WFLAGP(wwin, no_stickable)) */
603 action[i++] = net_wm_action_stick;
605 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
606 if (IS_RESIZABLE(wwin))
607 action[i++] = net_wm_action_maximize_horz;
609 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
610 if (IS_RESIZABLE(wwin))
611 action[i++] = net_wm_action_maximize_vert;
613 /* if (!WFLAGP(wwin, no_fullscreen)) */
614 action[i++] = net_wm_action_fullscreen;
616 /* if (!WFLAGP(wwin, no_change_desktop)) */
617 action[i++] = net_wm_action_change_desktop;
619 if (!WFLAGP(wwin, no_closable))
620 action[i++] = net_wm_action_close;
622 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
623 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
626 Bool wNETWMGetUsableArea(WScreen * scr, int head, WArea * area)
628 WReservedArea *cur;
629 WMRect rect;
631 if (!scr->netdata || !scr->netdata->strut)
632 return False;
634 area->x1 = area->y1 = area->x2 = area->y2 = 0;
636 for (cur = scr->netdata->strut; cur; cur = cur->next) {
637 WWindow *wwin = wWindowFor(cur->window);
638 if (wWindowTouchesHead(wwin, head)) {
639 if (cur->area.x1 > area->x1)
640 area->x1 = cur->area.x1;
641 if (cur->area.y1 > area->y1)
642 area->y1 = cur->area.y1;
643 if (cur->area.x2 > area->x2)
644 area->x2 = cur->area.x2;
645 if (cur->area.y2 > area->y2)
646 area->y2 = cur->area.y2;
650 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
651 return False;
653 rect = wGetRectForHead(scr, head);
655 area->x1 = rect.pos.x + area->x1;
656 area->x2 = rect.pos.x + rect.size.width - area->x2;
657 area->y1 = rect.pos.y + area->y1;
658 area->y2 = rect.pos.y + rect.size.height - area->y2;
660 return True;
663 static void updateClientList(WScreen * scr)
665 WWindow *wwin;
666 Window *windows;
667 int count;
669 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
671 count = 0;
672 wwin = scr->focused_window;
673 while (wwin) {
674 windows[count++] = wwin->client_win;
675 wwin = wwin->prev;
677 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
678 PropModeReplace, (unsigned char *)windows, count);
680 wfree(windows);
681 XFlush(dpy);
684 static void updateClientListStacking(WScreen * scr, WWindow * wwin_excl)
686 WWindow *wwin;
687 Window *client_list;
688 Window *client_list_reverse;
689 int client_count;
690 WCoreWindow *tmp;
691 WMBagIterator iter;
692 int i;
694 /* update client list */
695 i = scr->window_count + 1;
696 client_list = (Window *) wmalloc(sizeof(Window) * i);
697 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
699 client_count = 0;
700 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
701 while (tmp) {
702 wwin = wWindowFor(tmp->window);
703 /* wwin_excl is a window to exclude from the list
704 (e.g. it's now unmanaged) */
705 if (wwin && (wwin != wwin_excl))
706 client_list[client_count++] = wwin->client_win;
707 tmp = tmp->stacking->under;
711 for (i = 0; i < client_count; i++) {
712 Window w = client_list[client_count - i - 1];
713 client_list_reverse[i] = w;
716 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
717 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
719 wfree(client_list);
720 wfree(client_list_reverse);
722 XFlush(dpy);
725 static void updateWorkspaceCount(WScreen * scr)
726 { /* changeable */
727 long count;
729 count = scr->workspace_count;
731 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
732 32, PropModeReplace, (unsigned char *)&count, 1);
735 static void updateCurrentWorkspace(WScreen * scr)
736 { /* changeable */
737 long count;
739 count = scr->current_workspace;
741 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
742 PropModeReplace, (unsigned char *)&count, 1);
745 static void updateWorkspaceNames(WScreen * scr)
747 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
748 unsigned int i, len, curr_size;
750 pos = buf;
751 len = 0;
752 for (i = 0; i < scr->workspace_count; i++) {
753 curr_size = strlen(scr->workspaces[i]->name);
754 strcpy(pos, scr->workspaces[i]->name);
755 pos += (curr_size + 1);
756 len += (curr_size + 1);
759 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
760 PropModeReplace, (unsigned char *)buf, len);
763 static void updateFocusHint(WScreen * scr, WWindow * wwin)
764 { /* changeable */
765 Window window;
767 if (!scr->focused_window || !scr->focused_window->flags.focused)
768 window = None;
769 else
770 window = scr->focused_window->client_win;
772 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
773 PropModeReplace, (unsigned char *)&window, 1);
776 static void updateWorkspaceHint(WWindow * wwin, Bool fake, Bool del)
778 long l;
780 if (del) {
781 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
782 } else {
783 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
784 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
785 32, PropModeReplace, (unsigned char *)&l, 1);
789 static void updateStateHint(WWindow * wwin, Bool changedWorkspace, Bool del)
790 { /* changeable */
791 if (del) {
792 if (!wwin->flags.net_state_from_client) {
793 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
795 } else {
796 Atom state[15]; /* nr of defined state atoms */
797 int i = 0;
799 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
800 updateWorkspaceHint(wwin, False, False);
802 if (IS_OMNIPRESENT(wwin))
803 state[i++] = net_wm_state_sticky;
804 if (wwin->flags.shaded)
805 state[i++] = net_wm_state_shaded;
806 if (wwin->flags.maximized & MAX_HORIZONTAL)
807 state[i++] = net_wm_state_maximized_horz;
808 if (wwin->flags.maximized & MAX_VERTICAL)
809 state[i++] = net_wm_state_maximized_vert;
810 if (WFLAGP(wwin, skip_window_list))
811 state[i++] = net_wm_state_skip_taskbar;
812 if (wwin->flags.net_skip_pager)
813 state[i++] = net_wm_state_skip_pager;
814 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
815 state[i++] = net_wm_state_hidden;
816 state[i++] = net_wm_state_skip_pager;
818 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
819 if (!IS_OMNIPRESENT(wwin))
820 updateWorkspaceHint(wwin, True, False);
821 state[i++] = net_wm_state_sticky;
824 if (WFLAGP(wwin, sunken))
825 state[i++] = net_wm_state_below;
826 if (WFLAGP(wwin, floating))
827 state[i++] = net_wm_state_above;
828 if (wwin->flags.fullscreen)
829 state[i++] = net_wm_state_fullscreen;
831 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
832 PropModeReplace, (unsigned char *)state, i);
836 static Bool updateStrut(WWindow * wwin, Bool adding)
838 WScreen *scr;
839 WReservedArea *area;
840 Bool hasState = False;
842 scr = wwin->screen_ptr;
844 if (adding) {
845 Atom type_ret;
846 int fmt_ret;
847 unsigned long nitems_ret;
848 unsigned long bytes_after_ret;
849 long *data = NULL;
851 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
852 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
853 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
855 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
856 area->area.x1 = data[0];
857 area->area.x2 = data[1];
858 area->area.y1 = data[2];
859 area->area.y2 = data[3];
861 area->window = wwin->client_win;
863 area->next = scr->netdata->strut;
864 scr->netdata->strut = area;
866 XFree(data);
867 hasState = True;
870 } else {
871 /* deleting */
872 area = scr->netdata->strut;
874 if (area) {
875 if (area->window == wwin->client_win) {
876 scr->netdata->strut = area->next;
877 wfree(area);
878 hasState = True;
879 } else {
880 while (area->next && area->next->window != wwin->client_win)
881 area = area->next;
883 if (area->next) {
884 WReservedArea *next;
886 next = area->next->next;
887 wfree(area->next);
888 area->next = next;
890 hasState = True;
896 return hasState;
899 static int getWindowLayer(WWindow * wwin)
901 int layer = WMNormalLevel;
903 if (wwin->type == net_wm_window_type_desktop) {
904 layer = WMDesktopLevel;
905 } else if (wwin->type == net_wm_window_type_dock) {
906 layer = WMDockLevel;
907 } else if (wwin->type == net_wm_window_type_toolbar) {
908 layer = WMMainMenuLevel;
909 } else if (wwin->type == net_wm_window_type_menu) {
910 layer = WMSubmenuLevel;
911 } else if (wwin->type == net_wm_window_type_utility) {
912 } else if (wwin->type == net_wm_window_type_splash) {
913 } else if (wwin->type == net_wm_window_type_dialog) {
914 if (wwin->transient_for) {
915 WWindow *parent = wWindowFor(wwin->transient_for);
916 if (parent && parent->flags.fullscreen) {
917 layer = WMFullscreenLevel;
920 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
921 } else if (wwin->type == net_wm_window_type_normal) {
924 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
925 layer = WMSunkenLevel;
926 if (wwin->client_flags.floating && WMFloatingLevel > layer)
927 layer = WMFloatingLevel;
929 return layer;
932 static void doStateAtom(WWindow * wwin, Atom state, int set, Bool init)
935 if (state == net_wm_state_sticky) {
936 if (set == _NET_WM_STATE_TOGGLE) {
937 set = !IS_OMNIPRESENT(wwin);
939 if (set != wwin->flags.omnipresent) {
940 wWindowSetOmnipresent(wwin, set);
942 } else if (state == net_wm_state_shaded) {
943 if (set == _NET_WM_STATE_TOGGLE) {
944 set = !wwin->flags.shaded;
946 if (init) {
947 wwin->flags.shaded = set;
948 } else {
949 if (set) {
950 wShadeWindow(wwin);
951 } else {
952 wUnshadeWindow(wwin);
955 } else if (state == net_wm_state_skip_taskbar) {
956 if (set == _NET_WM_STATE_TOGGLE) {
957 set = !wwin->client_flags.skip_window_list;
959 wwin->client_flags.skip_window_list = set;
960 } else if (state == net_wm_state_skip_pager) {
961 if (set == _NET_WM_STATE_TOGGLE) {
962 set = !wwin->flags.net_skip_pager;
964 wwin->flags.net_skip_pager = set;
965 } else if (state == net_wm_state_maximized_vert) {
966 if (set == _NET_WM_STATE_TOGGLE) {
967 set = !(wwin->flags.maximized & MAX_VERTICAL);
969 if (init) {
970 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
971 } else {
972 if (set) {
973 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
974 } else {
975 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
978 } else if (state == net_wm_state_maximized_horz) {
979 if (set == _NET_WM_STATE_TOGGLE) {
980 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
982 if (init) {
983 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
984 } else {
985 if (set) {
986 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
987 } else {
988 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
991 } else if (state == net_wm_state_hidden) {
992 if (set == _NET_WM_STATE_TOGGLE) {
993 set = !(wwin->flags.miniaturized);
995 if (init) {
996 wwin->flags.miniaturized = set;
997 } else {
998 if (set) {
999 wIconifyWindow(wwin);
1000 } else {
1001 wDeiconifyWindow(wwin);
1004 } else if (state == net_wm_state_fullscreen) {
1005 if (set == _NET_WM_STATE_TOGGLE) {
1006 set = !(wwin->flags.fullscreen);
1008 if (init) {
1009 wwin->flags.fullscreen = set;
1010 } else {
1011 if (set) {
1012 wFullscreenWindow(wwin);
1013 } else {
1014 wUnfullscreenWindow(wwin);
1017 } else if (state == net_wm_state_above) {
1018 if (set == _NET_WM_STATE_TOGGLE) {
1019 set = !(wwin->client_flags.floating);
1021 if (init) {
1022 wwin->client_flags.floating = set;
1023 } else {
1024 wwin->client_flags.floating = set;
1025 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1028 } else if (state == net_wm_state_below) {
1029 if (set == _NET_WM_STATE_TOGGLE) {
1030 set = !(wwin->client_flags.sunken);
1032 if (init) {
1033 wwin->client_flags.sunken = set;
1034 } else {
1035 wwin->client_flags.sunken = set;
1036 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1039 } else {
1040 #ifdef DEBUG_WMSPEC
1041 wmessage("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1042 #endif
1046 static void removeIcon(WWindow * wwin)
1048 if (wwin->icon == NULL)
1049 return;
1050 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1051 XUnmapWindow(dpy, wwin->icon->core->window);
1052 RemoveFromStackList(wwin->icon->core);
1053 wIconDestroy(wwin->icon);
1054 wwin->icon = NULL;
1058 static Bool handleWindowType(WWindow * wwin, Atom type, int *layer)
1060 Bool ret = True;
1062 if (type == net_wm_window_type_desktop) {
1063 wwin->client_flags.no_titlebar = 1;
1064 wwin->client_flags.no_resizable = 1;
1065 wwin->client_flags.no_miniaturizable = 1;
1066 wwin->client_flags.no_border = 1;
1067 wwin->client_flags.no_resizebar = 1;
1068 wwin->client_flags.no_shadeable = 1;
1069 wwin->client_flags.no_movable = 1;
1070 wwin->client_flags.omnipresent = 1;
1071 wwin->client_flags.skip_window_list = 1;
1072 wwin->client_flags.skip_switchpanel = 1;
1073 wwin->client_flags.dont_move_off = 1;
1074 wwin->client_flags.no_appicon = 1;
1075 wwin->flags.net_skip_pager = 1;
1076 wwin->frame_x = 0;
1077 wwin->frame_y = 0;
1078 } else if (type == net_wm_window_type_dock) {
1079 wwin->client_flags.no_titlebar = 1;
1080 wwin->client_flags.no_resizable = 1;
1081 wwin->client_flags.no_miniaturizable = 1;
1082 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1083 wwin->client_flags.no_resizebar = 1;
1084 wwin->client_flags.no_shadeable = 1;
1085 wwin->client_flags.no_movable = 1;
1086 wwin->client_flags.omnipresent = 1;
1087 wwin->client_flags.skip_window_list = 1;
1088 wwin->client_flags.skip_switchpanel = 1;
1089 wwin->client_flags.dont_move_off = 1;
1090 wwin->flags.net_skip_pager = 1;
1091 } else if (type == net_wm_window_type_toolbar) {
1092 wwin->client_flags.no_titlebar = 1;
1093 wwin->client_flags.no_resizable = 1;
1094 wwin->client_flags.no_miniaturizable = 1;
1095 wwin->client_flags.no_resizebar = 1;
1096 wwin->client_flags.no_shadeable = 1;
1097 wwin->client_flags.skip_window_list = 1;
1098 wwin->client_flags.skip_switchpanel = 1;
1099 wwin->client_flags.dont_move_off = 1;
1100 wwin->client_flags.no_appicon = 1;
1101 } else if (type == net_wm_window_type_menu) {
1102 wwin->client_flags.no_titlebar = 1;
1103 wwin->client_flags.no_resizable = 1;
1104 wwin->client_flags.no_miniaturizable = 1;
1105 wwin->client_flags.no_resizebar = 1;
1106 wwin->client_flags.no_shadeable = 1;
1107 wwin->client_flags.skip_window_list = 1;
1108 wwin->client_flags.skip_switchpanel = 1;
1109 wwin->client_flags.dont_move_off = 1;
1110 wwin->client_flags.no_appicon = 1;
1111 } else if (type == net_wm_window_type_utility) {
1112 wwin->client_flags.no_appicon = 1;
1113 } else if (type == net_wm_window_type_splash) {
1114 wwin->client_flags.no_titlebar = 1;
1115 wwin->client_flags.no_resizable = 1;
1116 wwin->client_flags.no_miniaturizable = 1;
1117 wwin->client_flags.no_resizebar = 1;
1118 wwin->client_flags.no_shadeable = 1;
1119 wwin->client_flags.no_movable = 1;
1120 wwin->client_flags.skip_window_list = 1;
1121 wwin->client_flags.skip_switchpanel = 1;
1122 wwin->client_flags.dont_move_off = 1;
1123 wwin->client_flags.no_appicon = 1;
1124 wwin->flags.net_skip_pager = 1;
1125 } else if (type == net_wm_window_type_dialog) {
1126 /* These also seem a bad idea in our context -Dan
1127 // wwin->client_flags.skip_window_list = 1;
1128 // wwin->client_flags.no_appicon = 1;
1130 } else if (type == net_wm_window_type_normal) {
1131 } else {
1132 ret = False;
1135 wwin->type = type;
1136 *layer = getWindowLayer(wwin);
1138 return ret;
1141 void wNETWMPositionSplash(WWindow * wwin, int *x, int *y, int width, int height)
1143 if (wwin->type == net_wm_window_type_splash) {
1144 WScreen *scr = wwin->screen_ptr;
1145 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1146 *x = rect.pos.x + (rect.size.width - width) / 2;
1147 *y = rect.pos.y + (rect.size.height - height) / 2;
1151 static void updateWindowType(WWindow * wwin)
1153 Atom type_ret;
1154 int fmt_ret;
1155 unsigned long nitems_ret;
1156 unsigned long bytes_after_ret;
1157 long *data = NULL;
1158 int layer;
1159 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1160 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1161 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1163 int i;
1164 Atom *type = (Atom *) data;
1165 for (i = 0; i < nitems_ret; ++i) {
1166 if (handleWindowType(wwin, type[i], &layer))
1167 break;
1169 XFree(data);
1172 if (wwin->frame != NULL) {
1173 ChangeStackingLevel(wwin->frame->core, layer);
1174 wwin->frame->flags.need_texture_change = 1;
1175 wWindowConfigureBorders(wwin);
1176 wFrameWindowPaint(wwin->frame);
1177 wNETWMUpdateActions(wwin, False);
1181 Bool wNETWMCheckClientHints(WWindow * wwin, int *layer, int *workspace)
1183 Atom type_ret;
1184 int fmt_ret;
1185 unsigned long nitems_ret;
1186 unsigned long bytes_after_ret;
1187 long *data = NULL;
1188 Bool hasState = False;
1189 int i;
1191 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1192 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1193 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1195 long desktop = *data;
1196 XFree(data);
1198 if (desktop == -1)
1199 wwin->client_flags.omnipresent = 1;
1200 else
1201 *workspace = desktop;
1203 hasState = True;
1206 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1207 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1208 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1210 Atom *state = (Atom *) data;
1211 for (i = 0; i < nitems_ret; ++i) {
1212 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1214 XFree(data);
1215 hasState = True;
1218 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1219 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1220 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1222 Atom *type = (Atom *) data;
1223 for (i = 0; i < nitems_ret; ++i) {
1224 if (handleWindowType(wwin, type[i], layer))
1225 break;
1227 XFree(data);
1228 hasState = True;
1231 wNETWMUpdateActions(wwin, False);
1232 updateStrut(wwin, False);
1233 if (updateStrut(wwin, True)) {
1234 hasState = True;
1236 wScreenUpdateUsableArea(wwin->screen_ptr);
1238 return hasState;
1241 static Bool updateNetIconInfo(WWindow * wwin)
1244 Atom type_ret;
1245 int fmt_ret;
1246 unsigned long nitems_ret;
1247 unsigned long bytes_after_ret;
1248 long *data = NULL;
1249 Bool hasState = False;
1250 Bool old_state = wwin->flags.net_handle_icon;
1252 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1253 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1254 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1255 long handled = *data;
1256 wwin->flags.net_handle_icon = (handled != 0);
1257 XFree(data);
1258 hasState = True;
1260 } else
1261 wwin->flags.net_handle_icon = False;
1263 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1264 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1265 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1267 #ifdef NETWM_PROPER
1268 if (wwin->flags.net_handle_icon)
1269 #else
1270 wwin->flags.net_handle_icon = True;
1271 #endif
1273 wwin->icon_x = data[0];
1274 wwin->icon_y = data[1];
1275 wwin->icon_w = data[2];
1276 wwin->icon_h = data[3];
1279 XFree(data);
1280 hasState = True;
1282 } else
1283 wwin->flags.net_handle_icon = False;
1285 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1286 if (wwin->flags.net_handle_icon) {
1287 removeIcon(wwin);
1288 } else {
1289 wwin->flags.miniaturized = False;
1290 wwin->flags.skip_next_animation = True;
1291 wIconifyWindow(wwin);
1295 return hasState;
1298 Bool wNETWMCheckInitialClientState(WWindow * wwin)
1300 Bool hasState = False;
1302 #ifdef DEBUG_WMSPEC
1303 wmessage("enter");
1304 #endif
1306 wNETWMShowingDesktop(wwin->screen_ptr, False);
1308 hasState |= updateNetIconInfo(wwin);
1310 updateIconImage(wwin);
1312 return hasState;
1315 static void handleDesktopNames(XClientMessageEvent * event, WScreen * scr)
1317 unsigned long nitems_ret, bytes_after_ret;
1318 char *data, *names[32];
1319 int fmt_ret, i, n;
1320 Atom type_ret;
1322 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1323 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1324 &bytes_after_ret, (unsigned char **)&data) != Success) {
1325 return;
1328 if (data == NULL)
1329 return;
1331 if (type_ret != utf8_string || fmt_ret != 8)
1332 return;
1334 n = 0;
1335 names[n] = data;
1336 for (i = 0; i < nitems_ret; i++) {
1337 if (data[i] == 0) {
1338 n++;
1339 names[n] = &data[i];
1340 } else if (*names[n] == 0) {
1341 names[n] = &data[i];
1342 wWorkspaceRename(scr, n, names[n]);
1347 Bool wNETWMProcessClientMessage(XClientMessageEvent * event)
1349 WScreen *scr;
1350 WWindow *wwin;
1351 Bool done = True;
1353 #ifdef DEBUG_WMSPEC
1354 wmessage("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1355 #endif
1357 scr = wScreenForWindow(event->window);
1358 if (scr) {
1359 /* generic client messages */
1360 if (event->message_type == net_current_desktop) {
1361 wWorkspaceChange(scr, event->data.l[0]);
1362 } else if (event->message_type == net_number_of_desktops) {
1363 long value;
1365 value = event->data.l[0];
1366 if (value > scr->workspace_count) {
1367 wWorkspaceMake(scr, value - scr->workspace_count);
1368 } else if (value < scr->workspace_count) {
1369 int i;
1370 Bool rebuild = False;
1372 for (i = scr->workspace_count - 1; i >= value; i--) {
1373 if (!wWorkspaceDelete(scr, i)) {
1374 rebuild = True;
1375 break;
1379 if (rebuild) {
1380 updateWorkspaceCount(scr);
1383 } else if (event->message_type == net_showing_desktop) {
1384 wNETWMShowingDesktop(scr, event->data.l[0]);
1385 } else if (event->message_type == net_desktop_names) {
1386 handleDesktopNames(event, scr);
1387 } else {
1388 done = False;
1391 if (done)
1392 return True;
1395 /* window specific client messages */
1397 wwin = wWindowFor(event->window);
1398 if (!wwin)
1399 return False;
1401 if (event->message_type == net_active_window) {
1403 * Satisfy a client's focus request only if
1404 * - request comes from a pager, or
1405 * - it's explicitly allowed in Advanced Options, or
1406 * - giving the client the focus does not cause a change in
1407 * the active workspace (XXX: or the active head if Xinerama)
1409 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace /* No workspace change */
1410 || event->data.l[0] == 2 /* Requested by pager */
1411 || WFLAGP(wwin, focus_across_wksp) /* Explicitly allowed */) {
1412 wNETWMShowingDesktop(scr, False);
1413 wMakeWindowVisible(wwin);
1415 } else if (event->message_type == net_close_window) {
1416 if (!WFLAGP(wwin, no_closable)) {
1417 if (wwin->protocols.DELETE_WINDOW)
1418 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1420 } else if (event->message_type == net_wm_state) {
1421 int maximized = wwin->flags.maximized;
1422 long set = event->data.l[0];
1424 #ifdef DEBUG_WMSPEC
1425 wmessage("net_wm_state set %ld a1 %s a2 %s\n", set,
1426 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1427 #endif
1429 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1430 if (event->data.l[2])
1431 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1433 if (wwin->flags.maximized != maximized) {
1434 if (!wwin->flags.maximized) {
1435 wwin->flags.maximized = maximized;
1436 wUnmaximizeWindow(wwin);
1437 } else {
1438 wMaximizeWindow(wwin, wwin->flags.maximized);
1441 updateStateHint(wwin, False, False);
1442 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1443 updateNetIconInfo(wwin);
1444 } else if (event->message_type == net_wm_desktop) {
1445 long desktop = event->data.l[0];
1446 if (desktop == -1) {
1447 wWindowSetOmnipresent(wwin, True);
1448 } else {
1449 if (IS_OMNIPRESENT(wwin))
1450 wWindowSetOmnipresent(wwin, False);
1451 wWindowChangeWorkspace(wwin, desktop);
1453 } else {
1454 done = False;
1457 return done;
1460 Bool wNETWMCheckClientHintChange(WWindow * wwin, XPropertyEvent * event)
1462 Bool ret = True;
1464 #ifdef DEBUG_WMSPEC
1465 wmessage("clientHintChange type %s\n", XGetAtomName(dpy, event->atom));
1466 #endif
1468 if (event->atom == net_wm_strut) {
1469 updateStrut(wwin, False);
1470 updateStrut(wwin, True);
1471 wScreenUpdateUsableArea(wwin->screen_ptr);
1472 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1473 updateNetIconInfo(wwin);
1474 } else if (event->atom == net_wm_window_type) {
1475 updateWindowType(wwin);
1476 } else if (event->atom == net_wm_name) {
1477 char *name = wNETWMGetWindowName(wwin->client_win);
1478 wWindowUpdateName(wwin, name);
1479 if (name)
1480 wfree(name);
1481 } else if (event->atom == net_wm_icon_name) {
1482 if (wwin->icon) {
1483 char *name = wNETWMGetIconName(wwin->client_win);
1484 wIconChangeTitle(wwin->icon, name);
1486 } else if (event->atom == net_wm_icon) {
1487 updateIconImage(wwin);
1488 } else {
1489 ret = False;
1492 return ret;
1495 int wNETWMGetPidForWindow(Window window)
1497 Atom type_ret;
1498 int fmt_ret;
1499 unsigned long nitems_ret;
1500 unsigned long bytes_after_ret;
1501 long *data = NULL;
1502 int pid;
1504 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1505 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1506 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1508 pid = *data;
1509 XFree(data);
1510 } else {
1511 pid = 0;
1514 return pid;
1517 char *wNETWMGetWindowName(Window window)
1519 char *name;
1520 char *ret;
1521 int size;
1523 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1524 if (name) {
1525 ret = wstrndup(name, size);
1526 XFree(name);
1527 } else
1528 ret = NULL;
1529 return ret;
1532 char *wNETWMGetIconName(Window window)
1534 char *name;
1535 char *ret;
1536 int size;
1538 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1539 if (name) {
1540 ret = wstrndup(name, size);
1541 XFree(name);
1542 } else
1543 ret = NULL;
1544 return ret;
1547 static void observer(void *self, WMNotification * notif)
1549 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1550 const char *name = WMGetNotificationName(notif);
1551 void *data = WMGetNotificationClientData(notif);
1552 NetData *ndata = (NetData *) self;
1554 if (strcmp(name, WMNManaged) == 0 && wwin) {
1555 updateClientList(wwin->screen_ptr);
1556 updateClientListStacking(wwin->screen_ptr, NULL);
1557 updateStateHint(wwin, True, False);
1559 updateStrut(wwin, False);
1560 updateStrut(wwin, True);
1561 wScreenUpdateUsableArea(wwin->screen_ptr);
1562 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1563 updateClientList(wwin->screen_ptr);
1564 updateClientListStacking(wwin->screen_ptr, wwin);
1565 updateWorkspaceHint(wwin, False, True);
1566 updateStateHint(wwin, False, True);
1567 wNETWMUpdateActions(wwin, True);
1569 updateStrut(wwin, False);
1570 wScreenUpdateUsableArea(wwin->screen_ptr);
1571 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1572 updateClientListStacking(wwin->screen_ptr, NULL);
1573 updateStateHint(wwin, False, False);
1574 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1575 updateClientListStacking(wwin->screen_ptr, NULL);
1576 updateStateHint(wwin, False, False);
1577 } else if (strcmp(name, WMNChangedFocus) == 0) {
1578 updateFocusHint(ndata->scr, wwin);
1579 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1580 updateWorkspaceHint(wwin, False, False);
1581 updateStateHint(wwin, True, False);
1582 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1583 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1587 static void wsobserver(void *self, WMNotification * notif)
1589 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1590 const char *name = WMGetNotificationName(notif);
1592 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1593 updateWorkspaceCount(scr);
1594 updateWorkspaceNames(scr);
1595 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1596 updateWorkspaceCount(scr);
1597 updateWorkspaceNames(scr);
1598 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1599 updateCurrentWorkspace(scr);
1600 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1601 updateWorkspaceNames(scr);