Support _NET_FRAME_EXTENTS.
[wmaker-crm.git] / src / wmspec.c
blob494f55b3eac936b99ac4737f52dcecf79f578979
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 static Atom net_frame_extents;
125 /* Window Manager Protocols */
126 static Atom net_wm_ping; /* TODO */
128 static Atom utf8_string;
130 typedef struct {
131 char *name;
132 Atom *atom;
133 } atomitem_t;
135 static atomitem_t atomNames[] = {
136 {"_NET_SUPPORTED", &net_supported},
137 {"_NET_CLIENT_LIST", &net_client_list},
138 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
139 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
140 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
141 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
142 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
143 {"_NET_DESKTOP_NAMES", &net_desktop_names},
144 {"_NET_ACTIVE_WINDOW", &net_active_window},
145 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
146 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
147 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
148 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
150 {"_NET_CLOSE_WINDOW", &net_close_window},
151 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
152 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
154 {"_NET_WM_NAME", &net_wm_name},
155 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
156 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
157 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
158 {"_NET_WM_DESKTOP", &net_wm_desktop},
159 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
160 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
161 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
162 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
163 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
164 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
165 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
166 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
167 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
168 {"_NET_WM_STATE", &net_wm_state},
169 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
170 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
171 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
172 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
173 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
174 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
175 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
176 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
177 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
178 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
179 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
180 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
181 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
182 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
183 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
184 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
185 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
186 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
187 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
188 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
189 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
190 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
191 {"_NET_WM_STRUT", &net_wm_strut},
192 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
193 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
194 {"_NET_WM_ICON", &net_wm_icon},
195 {"_NET_WM_PID", &net_wm_pid},
196 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
198 {"_NET_FRAME_EXTENTS", &net_frame_extents},
200 {"_NET_WM_PING", &net_wm_ping},
202 {"UTF8_STRING", &utf8_string},
205 #define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
207 #define _NET_WM_STATE_REMOVE 0
208 #define _NET_WM_STATE_ADD 1
209 #define _NET_WM_STATE_TOGGLE 2
211 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
212 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
213 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
214 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
215 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
216 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
217 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
218 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
219 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
220 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
221 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
223 static void observer(void *self, WMNotification * notif);
224 static void wsobserver(void *self, WMNotification * notif);
226 static void updateClientList(WScreen * scr);
227 static void updateClientListStacking(WScreen * scr, WWindow *);
229 static void updateWorkspaceNames(WScreen * scr);
230 static void updateCurrentWorkspace(WScreen * scr);
231 static void updateWorkspaceCount(WScreen * scr);
233 typedef struct NetData {
234 WScreen *scr;
235 WReservedArea *strut;
236 WWindow **show_desktop;
237 } NetData;
239 static void setSupportedHints(WScreen * scr)
241 Atom atom[atomNr];
242 int i = 0;
244 /* set supported hints list */
245 /* XXX: extend this !!! */
247 atom[i++] = net_client_list;
248 atom[i++] = net_client_list_stacking;
249 atom[i++] = net_number_of_desktops;
250 atom[i++] = net_desktop_geometry;
251 atom[i++] = net_desktop_viewport;
252 atom[i++] = net_current_desktop;
253 atom[i++] = net_desktop_names;
254 atom[i++] = net_active_window;
255 atom[i++] = net_supporting_wm_check;
256 atom[i++] = net_showing_desktop;
257 #if 0
258 atom[i++] = net_wm_moveresize;
259 #endif
260 atom[i++] = net_wm_desktop;
261 atom[i++] = net_wm_window_type;
262 atom[i++] = net_wm_window_type_desktop;
263 atom[i++] = net_wm_window_type_dock;
264 atom[i++] = net_wm_window_type_toolbar;
265 atom[i++] = net_wm_window_type_menu;
266 atom[i++] = net_wm_window_type_utility;
267 atom[i++] = net_wm_window_type_splash;
268 atom[i++] = net_wm_window_type_dialog;
269 atom[i++] = net_wm_window_type_normal;
271 atom[i++] = net_wm_state;
272 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
273 atom[i++] = net_wm_state_sticky;
274 atom[i++] = net_wm_state_shaded;
275 atom[i++] = net_wm_state_maximized_horz;
276 atom[i++] = net_wm_state_maximized_vert;
277 atom[i++] = net_wm_state_skip_taskbar;
278 atom[i++] = net_wm_state_skip_pager;
279 atom[i++] = net_wm_state_hidden;
280 atom[i++] = net_wm_state_fullscreen;
281 atom[i++] = net_wm_state_above;
282 atom[i++] = net_wm_state_below;
284 atom[i++] = net_wm_allowed_actions;
285 atom[i++] = net_wm_action_move;
286 atom[i++] = net_wm_action_resize;
287 atom[i++] = net_wm_action_minimize;
288 atom[i++] = net_wm_action_shade;
289 atom[i++] = net_wm_action_stick;
290 atom[i++] = net_wm_action_maximize_horz;
291 atom[i++] = net_wm_action_maximize_vert;
292 atom[i++] = net_wm_action_fullscreen;
293 atom[i++] = net_wm_action_change_desktop;
294 atom[i++] = net_wm_action_close;
296 atom[i++] = net_wm_strut;
297 atom[i++] = net_wm_icon_geometry;
298 atom[i++] = net_wm_icon;
299 atom[i++] = net_wm_handled_icons;
301 atom[i++] = net_frame_extents;
303 atom[i++] = net_wm_name;
304 atom[i++] = net_wm_icon_name;
306 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
308 /* set supporting wm hint */
309 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
310 PropModeReplace, (unsigned char *)&scr->info_window, 1);
312 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
313 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
316 void wNETWMUpdateDesktop(WScreen * scr)
318 long *views, sizes[2];
319 int count, i;
321 if (scr->workspace_count == 0)
322 return;
324 count = scr->workspace_count * 2;
325 views = wmalloc(sizeof(long) * count);
326 /*memset(views, 0, sizeof(long) * count); */
327 sizes[0] = scr->scr_width;
328 sizes[1] = scr->scr_height;
330 for (i = 0; i < scr->workspace_count; i++) {
331 views[2 * i + 0] = 0;
332 views[2 * i + 1] = 0;
335 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
336 PropModeReplace, (unsigned char *)sizes, 2);
338 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
339 PropModeReplace, (unsigned char *)views, count);
341 wfree(views);
344 int wNETWMGetCurrentDesktopFromHint(WScreen * scr)
346 int count;
347 unsigned char *prop;
349 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
350 if (prop) {
351 int desktop = *(long *)prop;
352 XFree(prop);
353 return desktop;
355 return -1;
359 * Find the best icon to be used by Window Maker for appicon/miniwindows.
360 * Currently the algorithm is to take the image with the size closest
361 * to icon_size x icon_size, but never bigger than that.
363 * This algorithm is very poorly implemented and needs to be redone (it can
364 * easily select images with very large widths and very small heights over
365 * square images, if the area of the former is closer to the desired one).
367 * The logic can also be changed to accept bigger images and scale them down.
369 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
371 int size, wanted, d;
372 unsigned long i, distance;
373 unsigned long *icon;
375 /* better use only 75% of icon_size. For 64x64 this means 48x48
376 * This leaves room around the icon for the miniwindow title and
377 * results in better overall aesthetics -Dan */
378 wanted = wPreferences.icon_size * wPreferences.icon_size;
380 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
381 size = data[i] * data[i + 1];
382 if (size == 0)
383 break;
384 d = wanted - size;
385 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
386 distance = d;
387 icon = &data[i];
389 i += size + 2;
392 return icon;
395 static RImage *makeRImageFromARGBData(unsigned long *data)
397 int size, width, height, i;
398 RImage *image;
399 unsigned char *imgdata;
400 unsigned long pixel;
402 width = data[0];
403 height = data[1];
404 size = width * height;
406 if (size == 0)
407 return NULL;
409 image = RCreateImage(width, height, True);
411 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
412 pixel = data[i];
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 */
419 return image;
422 static RImage *get_wwindow_image_from_x11(WWindow *wwin)
424 RImage *image;
425 Atom type;
426 int format;
427 unsigned long items, rest;
428 unsigned long *property, *data;
430 /* Get the icon from X11 Window */
431 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
432 False, XA_CARDINAL, &type, &format, &items, &rest,
433 (unsigned char **)&property) != Success || !property)
434 return NULL;
436 if (type != XA_CARDINAL || format != 32 || items < 2) {
437 XFree(property);
438 return NULL;
441 /* Find the best icon */
442 data = findBestIcon(property, items);
443 if (!data) {
444 XFree(property);
445 return NULL;
448 /* Save the best icon in the X11 icon */
449 image = makeRImageFromARGBData(data);
451 XFree(property);
452 return image;
455 static void updateIconImage(WWindow *wwin)
457 /* Remove the icon image from X11 */
458 if (wwin->net_icon_image)
459 RReleaseImage(wwin->net_icon_image);
461 /* Save the icon in the X11 icon */
462 wwin->net_icon_image = get_wwindow_image_from_x11(wwin);
464 /* Refresh the Window Icon */
465 if (wwin->icon)
466 wIconUpdate(wwin->icon);
468 /* Refresh the application icon */
469 WApplication *app = wApplicationOf(wwin->main_window);
470 if (app && app->app_icon) {
471 wIconUpdate(app->app_icon->icon);
472 wAppIconPaint(app->app_icon);
476 static void updateShowDesktop(WScreen * scr, Bool show)
478 long foo;
480 foo = (show == True);
481 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
482 PropModeReplace, (unsigned char *)&foo, 1);
485 void wNETWMShowingDesktop(WScreen * scr, Bool show)
487 if (show && scr->netdata->show_desktop == NULL) {
488 WWindow *tmp, **wins;
489 int i = 0;
491 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
493 tmp = scr->focused_window;
494 while (tmp) {
495 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
497 wins[i++] = tmp;
498 tmp->flags.skip_next_animation = 1;
499 tmp->flags.net_show_desktop = 1;
500 wIconifyWindow(tmp);
503 tmp = tmp->prev;
505 wins[i++] = NULL;
507 scr->netdata->show_desktop = wins;
508 updateShowDesktop(scr, True);
509 } else if (scr->netdata->show_desktop != NULL) {
510 /* FIXME: get rid of workspace flashing ! */
511 int ws = scr->current_workspace;
512 WWindow **tmp;
513 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
514 wDeiconifyWindow(*tmp);
515 (*tmp)->flags.net_show_desktop = 0;
517 if (ws != scr->current_workspace)
518 wWorkspaceChange(scr, ws);
519 wfree(scr->netdata->show_desktop);
520 scr->netdata->show_desktop = NULL;
521 updateShowDesktop(scr, False);
525 void wNETWMInitStuff(WScreen * scr)
527 NetData *data;
528 int i;
530 #ifdef DEBUG_WMSPEC
531 wmessage("enter");
532 #endif
534 #ifdef HAVE_XINTERNATOMS
536 Atom atoms[atomNr];
537 char *names[atomNr];
539 for (i = 0; i < atomNr; ++i) {
540 names[i] = atomNames[i].name;
542 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
543 for (i = 0; i < atomNr; ++i) {
544 *atomNames[i].atom = atoms[i];
547 #else
548 for (i = 0; i < atomNr; i++) {
549 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
551 #endif
553 data = wmalloc(sizeof(NetData));
554 data->scr = scr;
555 data->strut = NULL;
556 data->show_desktop = NULL;
558 scr->netdata = data;
560 setSupportedHints(scr);
562 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
563 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
564 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
565 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
566 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
567 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
568 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
570 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
571 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
572 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
573 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
575 updateClientList(scr);
576 updateClientListStacking(scr, NULL);
577 updateWorkspaceCount(scr);
578 updateWorkspaceNames(scr);
579 updateShowDesktop(scr, False);
581 wScreenUpdateUsableArea(scr);
584 void wNETWMCleanup(WScreen * scr)
586 int i;
588 for (i = 0; i < atomNr; i++)
589 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
592 void wNETWMUpdateActions(WWindow * wwin, Bool del)
594 Atom action[10]; /* nr of actions atoms defined */
595 int i = 0;
597 if (del) {
598 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
599 return;
602 if (IS_MOVABLE(wwin))
603 action[i++] = net_wm_action_move;
605 if (IS_RESIZABLE(wwin))
606 action[i++] = net_wm_action_resize;
608 if (!WFLAGP(wwin, no_miniaturizable))
609 action[i++] = net_wm_action_minimize;
611 if (!WFLAGP(wwin, no_shadeable))
612 action[i++] = net_wm_action_shade;
614 /* if (!WFLAGP(wwin, no_stickable)) */
615 action[i++] = net_wm_action_stick;
617 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
618 if (IS_RESIZABLE(wwin))
619 action[i++] = net_wm_action_maximize_horz;
621 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
622 if (IS_RESIZABLE(wwin))
623 action[i++] = net_wm_action_maximize_vert;
625 /* if (!WFLAGP(wwin, no_fullscreen)) */
626 action[i++] = net_wm_action_fullscreen;
628 /* if (!WFLAGP(wwin, no_change_desktop)) */
629 action[i++] = net_wm_action_change_desktop;
631 if (!WFLAGP(wwin, no_closable))
632 action[i++] = net_wm_action_close;
634 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
635 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
638 Bool wNETWMGetUsableArea(WScreen * scr, int head, WArea * area)
640 WReservedArea *cur;
641 WMRect rect;
643 if (!scr->netdata || !scr->netdata->strut)
644 return False;
646 area->x1 = area->y1 = area->x2 = area->y2 = 0;
648 for (cur = scr->netdata->strut; cur; cur = cur->next) {
649 WWindow *wwin = wWindowFor(cur->window);
650 if (wWindowTouchesHead(wwin, head)) {
651 if (cur->area.x1 > area->x1)
652 area->x1 = cur->area.x1;
653 if (cur->area.y1 > area->y1)
654 area->y1 = cur->area.y1;
655 if (cur->area.x2 > area->x2)
656 area->x2 = cur->area.x2;
657 if (cur->area.y2 > area->y2)
658 area->y2 = cur->area.y2;
662 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
663 return False;
665 rect = wGetRectForHead(scr, head);
667 area->x1 = rect.pos.x + area->x1;
668 area->x2 = rect.pos.x + rect.size.width - area->x2;
669 area->y1 = rect.pos.y + area->y1;
670 area->y2 = rect.pos.y + rect.size.height - area->y2;
672 return True;
675 static void updateClientList(WScreen * scr)
677 WWindow *wwin;
678 Window *windows;
679 int count;
681 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
683 count = 0;
684 wwin = scr->focused_window;
685 while (wwin) {
686 windows[count++] = wwin->client_win;
687 wwin = wwin->prev;
689 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
690 PropModeReplace, (unsigned char *)windows, count);
692 wfree(windows);
693 XFlush(dpy);
696 static void updateClientListStacking(WScreen * scr, WWindow * wwin_excl)
698 WWindow *wwin;
699 Window *client_list;
700 Window *client_list_reverse;
701 int client_count;
702 WCoreWindow *tmp;
703 WMBagIterator iter;
704 int i;
706 /* update client list */
707 i = scr->window_count + 1;
708 client_list = (Window *) wmalloc(sizeof(Window) * i);
709 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
711 client_count = 0;
712 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
713 while (tmp) {
714 wwin = wWindowFor(tmp->window);
715 /* wwin_excl is a window to exclude from the list
716 (e.g. it's now unmanaged) */
717 if (wwin && (wwin != wwin_excl))
718 client_list[client_count++] = wwin->client_win;
719 tmp = tmp->stacking->under;
723 for (i = 0; i < client_count; i++) {
724 Window w = client_list[client_count - i - 1];
725 client_list_reverse[i] = w;
728 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
729 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
731 wfree(client_list);
732 wfree(client_list_reverse);
734 XFlush(dpy);
737 static void updateWorkspaceCount(WScreen * scr)
738 { /* changeable */
739 long count;
741 count = scr->workspace_count;
743 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
744 32, PropModeReplace, (unsigned char *)&count, 1);
747 static void updateCurrentWorkspace(WScreen * scr)
748 { /* changeable */
749 long count;
751 count = scr->current_workspace;
753 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
754 PropModeReplace, (unsigned char *)&count, 1);
757 static void updateWorkspaceNames(WScreen * scr)
759 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
760 unsigned int i, len, curr_size;
762 pos = buf;
763 len = 0;
764 for (i = 0; i < scr->workspace_count; i++) {
765 curr_size = strlen(scr->workspaces[i]->name);
766 strcpy(pos, scr->workspaces[i]->name);
767 pos += (curr_size + 1);
768 len += (curr_size + 1);
771 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
772 PropModeReplace, (unsigned char *)buf, len);
775 static void updateFocusHint(WScreen * scr, WWindow * wwin)
776 { /* changeable */
777 Window window;
779 if (!scr->focused_window || !scr->focused_window->flags.focused)
780 window = None;
781 else
782 window = scr->focused_window->client_win;
784 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
785 PropModeReplace, (unsigned char *)&window, 1);
788 static void updateWorkspaceHint(WWindow * wwin, Bool fake, Bool del)
790 long l;
792 if (del) {
793 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
794 } else {
795 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
796 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
797 32, PropModeReplace, (unsigned char *)&l, 1);
801 static void updateStateHint(WWindow * wwin, Bool changedWorkspace, Bool del)
802 { /* changeable */
803 if (del) {
804 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
805 } else {
806 Atom state[15]; /* nr of defined state atoms */
807 int i = 0;
809 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
810 updateWorkspaceHint(wwin, False, False);
812 if (IS_OMNIPRESENT(wwin))
813 state[i++] = net_wm_state_sticky;
814 if (wwin->flags.shaded)
815 state[i++] = net_wm_state_shaded;
816 if (wwin->flags.maximized & MAX_HORIZONTAL)
817 state[i++] = net_wm_state_maximized_horz;
818 if (wwin->flags.maximized & MAX_VERTICAL)
819 state[i++] = net_wm_state_maximized_vert;
820 if (WFLAGP(wwin, skip_window_list))
821 state[i++] = net_wm_state_skip_taskbar;
822 if (wwin->flags.net_skip_pager)
823 state[i++] = net_wm_state_skip_pager;
824 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
825 state[i++] = net_wm_state_hidden;
826 state[i++] = net_wm_state_skip_pager;
828 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
829 if (!IS_OMNIPRESENT(wwin))
830 updateWorkspaceHint(wwin, True, False);
831 state[i++] = net_wm_state_sticky;
834 if (WFLAGP(wwin, sunken))
835 state[i++] = net_wm_state_below;
836 if (WFLAGP(wwin, floating))
837 state[i++] = net_wm_state_above;
838 if (wwin->flags.fullscreen)
839 state[i++] = net_wm_state_fullscreen;
841 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
842 PropModeReplace, (unsigned char *)state, i);
846 static Bool updateStrut(WWindow * wwin, Bool adding)
848 WScreen *scr;
849 WReservedArea *area;
850 Bool hasState = False;
852 scr = wwin->screen_ptr;
854 if (adding) {
855 Atom type_ret;
856 int fmt_ret;
857 unsigned long nitems_ret;
858 unsigned long bytes_after_ret;
859 long *data = NULL;
861 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
862 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
863 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
865 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
866 area->area.x1 = data[0];
867 area->area.x2 = data[1];
868 area->area.y1 = data[2];
869 area->area.y2 = data[3];
871 area->window = wwin->client_win;
873 area->next = scr->netdata->strut;
874 scr->netdata->strut = area;
876 XFree(data);
877 hasState = True;
880 } else {
881 /* deleting */
882 area = scr->netdata->strut;
884 if (area) {
885 if (area->window == wwin->client_win) {
886 scr->netdata->strut = area->next;
887 wfree(area);
888 hasState = True;
889 } else {
890 while (area->next && area->next->window != wwin->client_win)
891 area = area->next;
893 if (area->next) {
894 WReservedArea *next;
896 next = area->next->next;
897 wfree(area->next);
898 area->next = next;
900 hasState = True;
906 return hasState;
909 static int getWindowLayer(WWindow * wwin)
911 int layer = WMNormalLevel;
913 if (wwin->type == net_wm_window_type_desktop) {
914 layer = WMDesktopLevel;
915 } else if (wwin->type == net_wm_window_type_dock) {
916 layer = WMDockLevel;
917 } else if (wwin->type == net_wm_window_type_toolbar) {
918 layer = WMMainMenuLevel;
919 } else if (wwin->type == net_wm_window_type_menu) {
920 layer = WMSubmenuLevel;
921 } else if (wwin->type == net_wm_window_type_utility) {
922 } else if (wwin->type == net_wm_window_type_splash) {
923 } else if (wwin->type == net_wm_window_type_dialog) {
924 if (wwin->transient_for) {
925 WWindow *parent = wWindowFor(wwin->transient_for);
926 if (parent && parent->flags.fullscreen) {
927 layer = WMFullscreenLevel;
930 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
931 } else if (wwin->type == net_wm_window_type_normal) {
934 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
935 layer = WMSunkenLevel;
936 if (wwin->client_flags.floating && WMFloatingLevel > layer)
937 layer = WMFloatingLevel;
939 return layer;
942 static void doStateAtom(WWindow * wwin, Atom state, int set, Bool init)
945 if (state == net_wm_state_sticky) {
946 if (set == _NET_WM_STATE_TOGGLE) {
947 set = !IS_OMNIPRESENT(wwin);
949 if (set != wwin->flags.omnipresent) {
950 wWindowSetOmnipresent(wwin, set);
952 } else if (state == net_wm_state_shaded) {
953 if (set == _NET_WM_STATE_TOGGLE) {
954 set = !wwin->flags.shaded;
956 if (init) {
957 wwin->flags.shaded = set;
958 } else {
959 if (set) {
960 wShadeWindow(wwin);
961 } else {
962 wUnshadeWindow(wwin);
965 } else if (state == net_wm_state_skip_taskbar) {
966 if (set == _NET_WM_STATE_TOGGLE) {
967 set = !wwin->client_flags.skip_window_list;
969 wwin->client_flags.skip_window_list = set;
970 } else if (state == net_wm_state_skip_pager) {
971 if (set == _NET_WM_STATE_TOGGLE) {
972 set = !wwin->flags.net_skip_pager;
974 wwin->flags.net_skip_pager = set;
975 } else if (state == net_wm_state_maximized_vert) {
976 if (set == _NET_WM_STATE_TOGGLE) {
977 set = !(wwin->flags.maximized & MAX_VERTICAL);
979 if (init) {
980 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
981 } else {
982 if (set) {
983 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
984 } else {
985 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
988 } else if (state == net_wm_state_maximized_horz) {
989 if (set == _NET_WM_STATE_TOGGLE) {
990 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
992 if (init) {
993 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
994 } else {
995 if (set) {
996 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
997 } else {
998 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1001 } else if (state == net_wm_state_hidden) {
1002 if (set == _NET_WM_STATE_TOGGLE) {
1003 set = !(wwin->flags.miniaturized);
1005 if (init) {
1006 wwin->flags.miniaturized = set;
1007 } else {
1008 if (set) {
1009 wIconifyWindow(wwin);
1010 } else {
1011 wDeiconifyWindow(wwin);
1014 } else if (state == net_wm_state_fullscreen) {
1015 if (set == _NET_WM_STATE_TOGGLE) {
1016 set = !(wwin->flags.fullscreen);
1018 if (init) {
1019 wwin->flags.fullscreen = set;
1020 } else {
1021 if (set) {
1022 wFullscreenWindow(wwin);
1023 } else {
1024 wUnfullscreenWindow(wwin);
1027 } else if (state == net_wm_state_above) {
1028 if (set == _NET_WM_STATE_TOGGLE) {
1029 set = !(wwin->client_flags.floating);
1031 if (init) {
1032 wwin->client_flags.floating = set;
1033 } else {
1034 wwin->client_flags.floating = set;
1035 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1038 } else if (state == net_wm_state_below) {
1039 if (set == _NET_WM_STATE_TOGGLE) {
1040 set = !(wwin->client_flags.sunken);
1042 if (init) {
1043 wwin->client_flags.sunken = set;
1044 } else {
1045 wwin->client_flags.sunken = set;
1046 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1049 } else {
1050 #ifdef DEBUG_WMSPEC
1051 wmessage("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1052 #endif
1056 static void removeIcon(WWindow * wwin)
1058 if (wwin->icon == NULL)
1059 return;
1060 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1061 XUnmapWindow(dpy, wwin->icon->core->window);
1062 RemoveFromStackList(wwin->icon->core);
1063 wIconDestroy(wwin->icon);
1064 wwin->icon = NULL;
1068 static Bool handleWindowType(WWindow * wwin, Atom type, int *layer)
1070 Bool ret = True;
1072 if (type == net_wm_window_type_desktop) {
1073 wwin->client_flags.no_titlebar = 1;
1074 wwin->client_flags.no_resizable = 1;
1075 wwin->client_flags.no_miniaturizable = 1;
1076 wwin->client_flags.no_border = 1;
1077 wwin->client_flags.no_resizebar = 1;
1078 wwin->client_flags.no_shadeable = 1;
1079 wwin->client_flags.no_movable = 1;
1080 wwin->client_flags.omnipresent = 1;
1081 wwin->client_flags.skip_window_list = 1;
1082 wwin->client_flags.skip_switchpanel = 1;
1083 wwin->client_flags.dont_move_off = 1;
1084 wwin->client_flags.no_appicon = 1;
1085 wwin->flags.net_skip_pager = 1;
1086 wwin->frame_x = 0;
1087 wwin->frame_y = 0;
1088 } else if (type == net_wm_window_type_dock) {
1089 wwin->client_flags.no_titlebar = 1;
1090 wwin->client_flags.no_resizable = 1;
1091 wwin->client_flags.no_miniaturizable = 1;
1092 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1093 wwin->client_flags.no_resizebar = 1;
1094 wwin->client_flags.no_shadeable = 1;
1095 wwin->client_flags.no_movable = 1;
1096 wwin->client_flags.omnipresent = 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->flags.net_skip_pager = 1;
1101 } else if (type == net_wm_window_type_toolbar) {
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_menu) {
1112 wwin->client_flags.no_titlebar = 1;
1113 wwin->client_flags.no_resizable = 1;
1114 wwin->client_flags.no_miniaturizable = 1;
1115 wwin->client_flags.no_resizebar = 1;
1116 wwin->client_flags.no_shadeable = 1;
1117 wwin->client_flags.skip_window_list = 1;
1118 wwin->client_flags.skip_switchpanel = 1;
1119 wwin->client_flags.dont_move_off = 1;
1120 wwin->client_flags.no_appicon = 1;
1121 } else if (type == net_wm_window_type_utility) {
1122 wwin->client_flags.no_appicon = 1;
1123 } else if (type == net_wm_window_type_splash) {
1124 wwin->client_flags.no_titlebar = 1;
1125 wwin->client_flags.no_resizable = 1;
1126 wwin->client_flags.no_miniaturizable = 1;
1127 wwin->client_flags.no_resizebar = 1;
1128 wwin->client_flags.no_shadeable = 1;
1129 wwin->client_flags.no_movable = 1;
1130 wwin->client_flags.skip_window_list = 1;
1131 wwin->client_flags.skip_switchpanel = 1;
1132 wwin->client_flags.dont_move_off = 1;
1133 wwin->client_flags.no_appicon = 1;
1134 wwin->flags.net_skip_pager = 1;
1135 } else if (type == net_wm_window_type_dialog) {
1136 /* These also seem a bad idea in our context -Dan
1137 // wwin->client_flags.skip_window_list = 1;
1138 // wwin->client_flags.no_appicon = 1;
1140 } else if (type == net_wm_window_type_normal) {
1141 } else {
1142 ret = False;
1145 wwin->type = type;
1146 *layer = getWindowLayer(wwin);
1148 return ret;
1151 void wNETWMPositionSplash(WWindow * wwin, int *x, int *y, int width, int height)
1153 if (wwin->type == net_wm_window_type_splash) {
1154 WScreen *scr = wwin->screen_ptr;
1155 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1156 *x = rect.pos.x + (rect.size.width - width) / 2;
1157 *y = rect.pos.y + (rect.size.height - height) / 2;
1161 static void updateWindowType(WWindow * wwin)
1163 Atom type_ret;
1164 int fmt_ret;
1165 unsigned long nitems_ret;
1166 unsigned long bytes_after_ret;
1167 long *data = NULL;
1168 int layer;
1169 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1170 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1171 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1173 int i;
1174 Atom *type = (Atom *) data;
1175 for (i = 0; i < nitems_ret; ++i) {
1176 if (handleWindowType(wwin, type[i], &layer))
1177 break;
1179 XFree(data);
1182 if (wwin->frame != NULL) {
1183 ChangeStackingLevel(wwin->frame->core, layer);
1184 wwin->frame->flags.need_texture_change = 1;
1185 wWindowConfigureBorders(wwin);
1186 wFrameWindowPaint(wwin->frame);
1187 wNETWMUpdateActions(wwin, False);
1191 Bool wNETWMCheckClientHints(WWindow * wwin, int *layer, int *workspace)
1193 Atom type_ret;
1194 int fmt_ret;
1195 unsigned long nitems_ret;
1196 unsigned long bytes_after_ret;
1197 long *data = NULL;
1198 Bool hasState = False;
1199 int i;
1201 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1202 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1203 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1205 long desktop = *data;
1206 XFree(data);
1208 if (desktop == -1)
1209 wwin->client_flags.omnipresent = 1;
1210 else
1211 *workspace = desktop;
1213 hasState = True;
1216 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1217 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1218 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1220 Atom *state = (Atom *) data;
1221 for (i = 0; i < nitems_ret; ++i) {
1222 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1224 XFree(data);
1225 hasState = True;
1228 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1229 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1230 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1232 Atom *type = (Atom *) data;
1233 for (i = 0; i < nitems_ret; ++i) {
1234 if (handleWindowType(wwin, type[i], layer))
1235 break;
1237 XFree(data);
1238 hasState = True;
1241 wNETWMUpdateActions(wwin, False);
1242 updateStrut(wwin, False);
1243 if (updateStrut(wwin, True)) {
1244 hasState = True;
1246 wScreenUpdateUsableArea(wwin->screen_ptr);
1248 return hasState;
1251 static Bool updateNetIconInfo(WWindow * wwin)
1254 Atom type_ret;
1255 int fmt_ret;
1256 unsigned long nitems_ret;
1257 unsigned long bytes_after_ret;
1258 long *data = NULL;
1259 Bool hasState = False;
1260 Bool old_state = wwin->flags.net_handle_icon;
1262 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1263 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1264 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1265 long handled = *data;
1266 wwin->flags.net_handle_icon = (handled != 0);
1267 XFree(data);
1268 hasState = True;
1270 } else
1271 wwin->flags.net_handle_icon = False;
1273 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1274 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1275 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1277 #ifdef NETWM_PROPER
1278 if (wwin->flags.net_handle_icon)
1279 #else
1280 wwin->flags.net_handle_icon = True;
1281 #endif
1283 wwin->icon_x = data[0];
1284 wwin->icon_y = data[1];
1285 wwin->icon_w = data[2];
1286 wwin->icon_h = data[3];
1289 XFree(data);
1290 hasState = True;
1292 } else
1293 wwin->flags.net_handle_icon = False;
1295 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1296 if (wwin->flags.net_handle_icon) {
1297 removeIcon(wwin);
1298 } else {
1299 wwin->flags.miniaturized = False;
1300 wwin->flags.skip_next_animation = True;
1301 wIconifyWindow(wwin);
1305 return hasState;
1308 Bool wNETWMCheckInitialClientState(WWindow * wwin)
1310 Bool hasState = False;
1312 #ifdef DEBUG_WMSPEC
1313 wmessage("enter");
1314 #endif
1316 wNETWMShowingDesktop(wwin->screen_ptr, False);
1318 hasState |= updateNetIconInfo(wwin);
1320 updateIconImage(wwin);
1322 return hasState;
1325 static void handleDesktopNames(XClientMessageEvent * event, WScreen * scr)
1327 unsigned long nitems_ret, bytes_after_ret;
1328 char *data, *names[32];
1329 int fmt_ret, i, n;
1330 Atom type_ret;
1332 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1333 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1334 &bytes_after_ret, (unsigned char **)&data) != Success) {
1335 return;
1338 if (data == NULL)
1339 return;
1341 if (type_ret != utf8_string || fmt_ret != 8)
1342 return;
1344 n = 0;
1345 names[n] = data;
1346 for (i = 0; i < nitems_ret; i++) {
1347 if (data[i] == 0) {
1348 n++;
1349 names[n] = &data[i];
1350 } else if (*names[n] == 0) {
1351 names[n] = &data[i];
1352 wWorkspaceRename(scr, n, names[n]);
1357 Bool wNETWMProcessClientMessage(XClientMessageEvent * event)
1359 WScreen *scr;
1360 WWindow *wwin;
1361 Bool done = True;
1363 #ifdef DEBUG_WMSPEC
1364 wmessage("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1365 #endif
1367 scr = wScreenForWindow(event->window);
1368 if (scr) {
1369 /* generic client messages */
1370 if (event->message_type == net_current_desktop) {
1371 wWorkspaceChange(scr, event->data.l[0]);
1372 } else if (event->message_type == net_number_of_desktops) {
1373 long value;
1375 value = event->data.l[0];
1376 if (value > scr->workspace_count) {
1377 wWorkspaceMake(scr, value - scr->workspace_count);
1378 } else if (value < scr->workspace_count) {
1379 int i;
1380 Bool rebuild = False;
1382 for (i = scr->workspace_count - 1; i >= value; i--) {
1383 if (!wWorkspaceDelete(scr, i)) {
1384 rebuild = True;
1385 break;
1389 if (rebuild) {
1390 updateWorkspaceCount(scr);
1393 } else if (event->message_type == net_showing_desktop) {
1394 wNETWMShowingDesktop(scr, event->data.l[0]);
1395 } else if (event->message_type == net_desktop_names) {
1396 handleDesktopNames(event, scr);
1397 } else {
1398 done = False;
1401 if (done)
1402 return True;
1405 /* window specific client messages */
1407 wwin = wWindowFor(event->window);
1408 if (!wwin)
1409 return False;
1411 if (event->message_type == net_active_window) {
1413 * Satisfy a client's focus request only if
1414 * - request comes from a pager, or
1415 * - it's explicitly allowed in Advanced Options, or
1416 * - giving the client the focus does not cause a change in
1417 * the active workspace (XXX: or the active head if Xinerama)
1419 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace /* No workspace change */
1420 || event->data.l[0] == 2 /* Requested by pager */
1421 || WFLAGP(wwin, focus_across_wksp) /* Explicitly allowed */) {
1422 wNETWMShowingDesktop(scr, False);
1423 wMakeWindowVisible(wwin);
1425 } else if (event->message_type == net_close_window) {
1426 if (!WFLAGP(wwin, no_closable)) {
1427 if (wwin->protocols.DELETE_WINDOW)
1428 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1430 } else if (event->message_type == net_wm_state) {
1431 int maximized = wwin->flags.maximized;
1432 long set = event->data.l[0];
1434 #ifdef DEBUG_WMSPEC
1435 wmessage("net_wm_state set %ld a1 %s a2 %s\n", set,
1436 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1437 #endif
1439 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1440 if (event->data.l[2])
1441 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1443 if (wwin->flags.maximized != maximized) {
1444 if (!wwin->flags.maximized) {
1445 wwin->flags.maximized = maximized;
1446 wUnmaximizeWindow(wwin);
1447 } else {
1448 wMaximizeWindow(wwin, wwin->flags.maximized);
1451 updateStateHint(wwin, False, False);
1452 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1453 updateNetIconInfo(wwin);
1454 } else if (event->message_type == net_wm_desktop) {
1455 long desktop = event->data.l[0];
1456 if (desktop == -1) {
1457 wWindowSetOmnipresent(wwin, True);
1458 } else {
1459 if (IS_OMNIPRESENT(wwin))
1460 wWindowSetOmnipresent(wwin, False);
1461 wWindowChangeWorkspace(wwin, desktop);
1463 } else {
1464 done = False;
1467 return done;
1470 Bool wNETWMCheckClientHintChange(WWindow * wwin, XPropertyEvent * event)
1472 Bool ret = True;
1474 #ifdef DEBUG_WMSPEC
1475 wmessage("clientHintChange type %s\n", XGetAtomName(dpy, event->atom));
1476 #endif
1478 if (event->atom == net_wm_strut) {
1479 updateStrut(wwin, False);
1480 updateStrut(wwin, True);
1481 wScreenUpdateUsableArea(wwin->screen_ptr);
1482 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1483 updateNetIconInfo(wwin);
1484 } else if (event->atom == net_wm_window_type) {
1485 updateWindowType(wwin);
1486 } else if (event->atom == net_wm_name) {
1487 char *name = wNETWMGetWindowName(wwin->client_win);
1488 wWindowUpdateName(wwin, name);
1489 if (name)
1490 wfree(name);
1491 } else if (event->atom == net_wm_icon_name) {
1492 if (wwin->icon) {
1493 char *name = wNETWMGetIconName(wwin->client_win);
1494 wIconChangeTitle(wwin->icon, name);
1496 } else if (event->atom == net_wm_icon) {
1497 updateIconImage(wwin);
1498 } else {
1499 ret = False;
1502 return ret;
1505 int wNETWMGetPidForWindow(Window window)
1507 Atom type_ret;
1508 int fmt_ret;
1509 unsigned long nitems_ret;
1510 unsigned long bytes_after_ret;
1511 long *data = NULL;
1512 int pid;
1514 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1515 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1516 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1518 pid = *data;
1519 XFree(data);
1520 } else {
1521 pid = 0;
1524 return pid;
1527 char *wNETWMGetWindowName(Window window)
1529 char *name;
1530 char *ret;
1531 int size;
1533 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1534 if (name) {
1535 ret = wstrndup(name, size);
1536 XFree(name);
1537 } else
1538 ret = NULL;
1539 return ret;
1542 char *wNETWMGetIconName(Window window)
1544 char *name;
1545 char *ret;
1546 int size;
1548 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1549 if (name) {
1550 ret = wstrndup(name, size);
1551 XFree(name);
1552 } else
1553 ret = NULL;
1554 return ret;
1557 static void observer(void *self, WMNotification * notif)
1559 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1560 const char *name = WMGetNotificationName(notif);
1561 void *data = WMGetNotificationClientData(notif);
1562 NetData *ndata = (NetData *) self;
1564 if (strcmp(name, WMNManaged) == 0 && wwin) {
1565 updateClientList(wwin->screen_ptr);
1566 updateClientListStacking(wwin->screen_ptr, NULL);
1567 updateStateHint(wwin, True, False);
1569 updateStrut(wwin, False);
1570 updateStrut(wwin, True);
1571 wScreenUpdateUsableArea(wwin->screen_ptr);
1572 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1573 updateClientList(wwin->screen_ptr);
1574 updateClientListStacking(wwin->screen_ptr, wwin);
1575 updateWorkspaceHint(wwin, False, True);
1576 updateStateHint(wwin, False, True);
1577 wNETWMUpdateActions(wwin, True);
1579 updateStrut(wwin, False);
1580 wScreenUpdateUsableArea(wwin->screen_ptr);
1581 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1582 updateClientListStacking(wwin->screen_ptr, NULL);
1583 updateStateHint(wwin, False, False);
1584 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1585 updateClientListStacking(wwin->screen_ptr, NULL);
1586 updateStateHint(wwin, False, False);
1587 } else if (strcmp(name, WMNChangedFocus) == 0) {
1588 updateFocusHint(ndata->scr, wwin);
1589 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1590 updateWorkspaceHint(wwin, False, False);
1591 updateStateHint(wwin, True, False);
1592 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1593 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1597 static void wsobserver(void *self, WMNotification * notif)
1599 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1600 const char *name = WMGetNotificationName(notif);
1602 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1603 updateWorkspaceCount(scr);
1604 updateWorkspaceNames(scr);
1605 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1606 updateWorkspaceCount(scr);
1607 updateWorkspaceNames(scr);
1608 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1609 updateCurrentWorkspace(scr);
1610 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1611 updateWorkspaceNames(scr);
1615 void wNETFrameExtents(WWindow *wwin)
1617 long extents[4] = { 0, 0, 0, 0 };
1619 /* The extents array describes dimensions which are not
1620 * part of the client window. In our case that means
1621 * widths of the border and heights of the titlebar and resizebar.
1623 * Index 0 = left
1624 * 1 = right
1625 * 2 = top
1626 * 3 = bottom
1628 if (!wwin->client_flags.no_border)
1629 extents[0] = extents[1] = FRAME_BORDER_WIDTH;
1630 if (wwin->frame->titlebar)
1631 extents[2] = wwin->frame->titlebar->height;
1632 if (wwin->frame->resizebar)
1633 extents[3] = wwin->frame->resizebar->height;
1635 XChangeProperty(dpy, wwin->client_win, net_frame_extents, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) extents, 4);