1e700897d13f204e5d97c1af3dc50f03f81137d9
[wmaker-crm.git] / src / wmspec.c
blob1e700897d13f204e5d97c1af3dc50f03f81137d9
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);
232 static void wNETWMShowingDesktop(WScreen *scr, Bool show);
234 typedef struct NetData {
235 WScreen *scr;
236 WReservedArea *strut;
237 WWindow **show_desktop;
238 } NetData;
240 static void setSupportedHints(WScreen *scr)
242 Atom atom[atomNr];
243 int i = 0;
245 /* set supported hints list */
246 /* XXX: extend this !!! */
248 atom[i++] = net_client_list;
249 atom[i++] = net_client_list_stacking;
250 atom[i++] = net_number_of_desktops;
251 atom[i++] = net_desktop_geometry;
252 atom[i++] = net_desktop_viewport;
253 atom[i++] = net_current_desktop;
254 atom[i++] = net_desktop_names;
255 atom[i++] = net_active_window;
256 atom[i++] = net_supporting_wm_check;
257 atom[i++] = net_showing_desktop;
258 #if 0
259 atom[i++] = net_wm_moveresize;
260 #endif
261 atom[i++] = net_wm_desktop;
262 atom[i++] = net_wm_window_type;
263 atom[i++] = net_wm_window_type_desktop;
264 atom[i++] = net_wm_window_type_dock;
265 atom[i++] = net_wm_window_type_toolbar;
266 atom[i++] = net_wm_window_type_menu;
267 atom[i++] = net_wm_window_type_utility;
268 atom[i++] = net_wm_window_type_splash;
269 atom[i++] = net_wm_window_type_dialog;
270 atom[i++] = net_wm_window_type_normal;
272 atom[i++] = net_wm_state;
273 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
274 atom[i++] = net_wm_state_sticky;
275 atom[i++] = net_wm_state_shaded;
276 atom[i++] = net_wm_state_maximized_horz;
277 atom[i++] = net_wm_state_maximized_vert;
278 atom[i++] = net_wm_state_skip_taskbar;
279 atom[i++] = net_wm_state_skip_pager;
280 atom[i++] = net_wm_state_hidden;
281 atom[i++] = net_wm_state_fullscreen;
282 atom[i++] = net_wm_state_above;
283 atom[i++] = net_wm_state_below;
285 atom[i++] = net_wm_allowed_actions;
286 atom[i++] = net_wm_action_move;
287 atom[i++] = net_wm_action_resize;
288 atom[i++] = net_wm_action_minimize;
289 atom[i++] = net_wm_action_shade;
290 atom[i++] = net_wm_action_stick;
291 atom[i++] = net_wm_action_maximize_horz;
292 atom[i++] = net_wm_action_maximize_vert;
293 atom[i++] = net_wm_action_fullscreen;
294 atom[i++] = net_wm_action_change_desktop;
295 atom[i++] = net_wm_action_close;
297 atom[i++] = net_wm_strut;
298 atom[i++] = net_wm_icon_geometry;
299 atom[i++] = net_wm_icon;
300 atom[i++] = net_wm_handled_icons;
302 atom[i++] = net_frame_extents;
304 atom[i++] = net_wm_name;
305 atom[i++] = net_wm_icon_name;
307 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
309 /* set supporting wm hint */
310 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
311 PropModeReplace, (unsigned char *)&scr->info_window, 1);
313 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
314 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
317 void wNETWMUpdateDesktop(WScreen *scr)
319 long *views, sizes[2];
320 int count, i;
322 if (scr->workspace_count == 0)
323 return;
325 count = scr->workspace_count * 2;
326 views = wmalloc(sizeof(long) * count);
327 /*memset(views, 0, sizeof(long) * count); */
328 sizes[0] = scr->scr_width;
329 sizes[1] = scr->scr_height;
331 for (i = 0; i < scr->workspace_count; i++) {
332 views[2 * i + 0] = 0;
333 views[2 * i + 1] = 0;
336 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
337 PropModeReplace, (unsigned char *)sizes, 2);
339 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
340 PropModeReplace, (unsigned char *)views, count);
342 wfree(views);
345 int wNETWMGetCurrentDesktopFromHint(WScreen *scr)
347 int count;
348 unsigned char *prop;
350 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
351 if (prop) {
352 int desktop = *(long *)prop;
353 XFree(prop);
354 return desktop;
356 return -1;
360 * Find the best icon to be used by Window Maker for appicon/miniwindows.
361 * Currently the algorithm is to take the image with the size closest
362 * to icon_size x icon_size, but never bigger than that.
364 * This algorithm is very poorly implemented and needs to be redone (it can
365 * easily select images with very large widths and very small heights over
366 * square images, if the area of the former is closer to the desired one).
368 * The logic can also be changed to accept bigger images and scale them down.
370 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
372 int size, wanted, d;
373 unsigned long i, distance;
374 unsigned long *icon;
376 /* better use only 75% of icon_size. For 64x64 this means 48x48
377 * This leaves room around the icon for the miniwindow title and
378 * results in better overall aesthetics -Dan */
379 wanted = wPreferences.icon_size * wPreferences.icon_size;
381 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
382 size = data[i] * data[i + 1];
383 if (size == 0)
384 break;
385 d = wanted - size;
386 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
387 distance = d;
388 icon = &data[i];
390 i += size + 2;
393 return icon;
396 static RImage *makeRImageFromARGBData(unsigned long *data)
398 int size, width, height, i;
399 RImage *image;
400 unsigned char *imgdata;
401 unsigned long pixel;
403 width = data[0];
404 height = data[1];
405 size = width * height;
407 if (size == 0)
408 return NULL;
410 image = RCreateImage(width, height, True);
412 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
413 pixel = data[i];
414 imgdata[3] = (pixel >> 24) & 0xff; /* A */
415 imgdata[0] = (pixel >> 16) & 0xff; /* R */
416 imgdata[1] = (pixel >> 8) & 0xff; /* G */
417 imgdata[2] = (pixel >> 0) & 0xff; /* B */
420 return image;
423 static RImage *get_wwindow_image_from_x11(WWindow *wwin)
425 RImage *image;
426 Atom type;
427 int format;
428 unsigned long items, rest;
429 unsigned long *property, *data;
431 /* Get the icon from X11 Window */
432 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
433 False, XA_CARDINAL, &type, &format, &items, &rest,
434 (unsigned char **)&property) != Success || !property)
435 return NULL;
437 if (type != XA_CARDINAL || format != 32 || items < 2) {
438 XFree(property);
439 return NULL;
442 /* Find the best icon */
443 data = findBestIcon(property, items);
444 if (!data) {
445 XFree(property);
446 return NULL;
449 /* Save the best icon in the X11 icon */
450 image = makeRImageFromARGBData(data);
452 XFree(property);
454 /* Resize the image to the correct value */
455 image = wIconValidateIconSize(image, wPreferences.icon_size);
457 return image;
460 static void updateIconImage(WWindow *wwin)
462 /* Remove the icon image from X11 */
463 if (wwin->net_icon_image)
464 RReleaseImage(wwin->net_icon_image);
466 /* Save the icon in the X11 icon */
467 wwin->net_icon_image = get_wwindow_image_from_x11(wwin);
469 /* Refresh the Window Icon */
470 if (wwin->icon)
471 wIconUpdate(wwin->icon);
473 /* Refresh the application icon */
474 WApplication *app = wApplicationOf(wwin->main_window);
475 if (app && app->app_icon) {
476 wIconUpdate(app->app_icon->icon);
477 wAppIconPaint(app->app_icon);
481 static void updateShowDesktop(WScreen *scr, Bool show)
483 long foo;
485 foo = (show == True);
486 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
487 PropModeReplace, (unsigned char *)&foo, 1);
490 static void wNETWMShowingDesktop(WScreen *scr, Bool show)
492 if (show && scr->netdata->show_desktop == NULL) {
493 WWindow *tmp, **wins;
494 int i = 0;
496 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
498 tmp = scr->focused_window;
499 while (tmp) {
500 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
502 wins[i++] = tmp;
503 tmp->flags.skip_next_animation = 1;
504 tmp->flags.net_show_desktop = 1;
505 wIconifyWindow(tmp);
508 tmp = tmp->prev;
510 wins[i++] = NULL;
512 scr->netdata->show_desktop = wins;
513 updateShowDesktop(scr, True);
514 } else if (scr->netdata->show_desktop != NULL) {
515 /* FIXME: get rid of workspace flashing ! */
516 int ws = scr->current_workspace;
517 WWindow **tmp;
518 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
519 wDeiconifyWindow(*tmp);
520 (*tmp)->flags.net_show_desktop = 0;
522 if (ws != scr->current_workspace)
523 wWorkspaceChange(scr, ws);
524 wfree(scr->netdata->show_desktop);
525 scr->netdata->show_desktop = NULL;
526 updateShowDesktop(scr, False);
530 void wNETWMInitStuff(WScreen *scr)
532 NetData *data;
533 int i;
535 #ifdef DEBUG_WMSPEC
536 wmessage("wNETWMInitStuff");
537 #endif
539 #ifdef HAVE_XINTERNATOMS
541 Atom atoms[atomNr];
542 char *names[atomNr];
544 for (i = 0; i < atomNr; ++i)
545 names[i] = atomNames[i].name;
547 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
548 for (i = 0; i < atomNr; ++i)
549 *atomNames[i].atom = atoms[i];
552 #else
553 for (i = 0; i < atomNr; i++)
554 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
555 #endif
557 data = wmalloc(sizeof(NetData));
558 data->scr = scr;
559 data->strut = NULL;
560 data->show_desktop = NULL;
562 scr->netdata = data;
564 setSupportedHints(scr);
566 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
567 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
568 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
569 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
570 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
571 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
572 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
574 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
575 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
576 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
577 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
579 updateClientList(scr);
580 updateClientListStacking(scr, NULL);
581 updateWorkspaceCount(scr);
582 updateWorkspaceNames(scr);
583 updateShowDesktop(scr, False);
585 wScreenUpdateUsableArea(scr);
588 void wNETWMCleanup(WScreen *scr)
590 int i;
592 for (i = 0; i < atomNr; i++)
593 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
596 void wNETWMUpdateActions(WWindow *wwin, Bool del)
598 Atom action[10]; /* nr of actions atoms defined */
599 int i = 0;
601 if (del) {
602 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
603 return;
606 if (IS_MOVABLE(wwin))
607 action[i++] = net_wm_action_move;
609 if (IS_RESIZABLE(wwin))
610 action[i++] = net_wm_action_resize;
612 if (!WFLAGP(wwin, no_miniaturizable))
613 action[i++] = net_wm_action_minimize;
615 if (!WFLAGP(wwin, no_shadeable))
616 action[i++] = net_wm_action_shade;
618 /* if (!WFLAGP(wwin, no_stickable)) */
619 action[i++] = net_wm_action_stick;
621 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
622 if (IS_RESIZABLE(wwin))
623 action[i++] = net_wm_action_maximize_horz;
625 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
626 if (IS_RESIZABLE(wwin))
627 action[i++] = net_wm_action_maximize_vert;
629 /* if (!WFLAGP(wwin, no_fullscreen)) */
630 action[i++] = net_wm_action_fullscreen;
632 /* if (!WFLAGP(wwin, no_change_desktop)) */
633 action[i++] = net_wm_action_change_desktop;
635 if (!WFLAGP(wwin, no_closable))
636 action[i++] = net_wm_action_close;
638 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
639 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
642 Bool wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
644 WReservedArea *cur;
645 WMRect rect;
647 if (!scr->netdata || !scr->netdata->strut)
648 return False;
650 area->x1 = area->y1 = area->x2 = area->y2 = 0;
652 for (cur = scr->netdata->strut; cur; cur = cur->next) {
653 WWindow *wwin = wWindowFor(cur->window);
654 if (wWindowTouchesHead(wwin, head)) {
655 if (cur->area.x1 > area->x1)
656 area->x1 = cur->area.x1;
657 if (cur->area.y1 > area->y1)
658 area->y1 = cur->area.y1;
659 if (cur->area.x2 > area->x2)
660 area->x2 = cur->area.x2;
661 if (cur->area.y2 > area->y2)
662 area->y2 = cur->area.y2;
666 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
667 return False;
669 rect = wGetRectForHead(scr, head);
671 area->x1 = rect.pos.x + area->x1;
672 area->x2 = rect.pos.x + rect.size.width - area->x2;
673 area->y1 = rect.pos.y + area->y1;
674 area->y2 = rect.pos.y + rect.size.height - area->y2;
676 return True;
679 static void updateClientList(WScreen *scr)
681 WWindow *wwin;
682 Window *windows;
683 int count;
685 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
687 count = 0;
688 wwin = scr->focused_window;
689 while (wwin) {
690 windows[count++] = wwin->client_win;
691 wwin = wwin->prev;
693 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
694 PropModeReplace, (unsigned char *)windows, count);
696 wfree(windows);
697 XFlush(dpy);
700 static void updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
702 WWindow *wwin;
703 Window *client_list, *client_list_reverse;
704 int client_count, i;
705 WCoreWindow *tmp;
706 WMBagIterator iter;
708 /* update client list */
709 i = scr->window_count + 1;
710 client_list = (Window *) wmalloc(sizeof(Window) * i);
711 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
713 client_count = 0;
714 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
715 while (tmp) {
716 wwin = wWindowFor(tmp->window);
717 /* wwin_excl is a window to exclude from the list
718 (e.g. it's now unmanaged) */
719 if (wwin && (wwin != wwin_excl))
720 client_list[client_count++] = wwin->client_win;
721 tmp = tmp->stacking->under;
725 for (i = 0; i < client_count; i++) {
726 Window w = client_list[client_count - i - 1];
727 client_list_reverse[i] = w;
730 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
731 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
733 wfree(client_list);
734 wfree(client_list_reverse);
736 XFlush(dpy);
739 static void updateWorkspaceCount(WScreen *scr)
740 { /* changeable */
741 long count;
743 count = scr->workspace_count;
745 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
746 32, PropModeReplace, (unsigned char *)&count, 1);
749 static void updateCurrentWorkspace(WScreen *scr)
750 { /* changeable */
751 long count;
753 count = scr->current_workspace;
755 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
756 PropModeReplace, (unsigned char *)&count, 1);
759 static void updateWorkspaceNames(WScreen *scr)
761 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
762 unsigned int i, len, curr_size;
764 pos = buf;
765 len = 0;
766 for (i = 0; i < scr->workspace_count; i++) {
767 curr_size = strlen(scr->workspaces[i]->name);
768 strcpy(pos, scr->workspaces[i]->name);
769 pos += (curr_size + 1);
770 len += (curr_size + 1);
773 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
774 PropModeReplace, (unsigned char *)buf, len);
777 static void updateFocusHint(WScreen *scr, WWindow *wwin)
778 { /* changeable */
779 Window window;
781 if (!scr->focused_window || !scr->focused_window->flags.focused)
782 window = None;
783 else
784 window = scr->focused_window->client_win;
786 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
787 PropModeReplace, (unsigned char *)&window, 1);
790 static void updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
792 long l;
794 if (del) {
795 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
796 } else {
797 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
798 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
799 32, PropModeReplace, (unsigned char *)&l, 1);
803 static void updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del)
804 { /* changeable */
805 if (del) {
806 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
807 } else {
808 Atom state[15]; /* nr of defined state atoms */
809 int i = 0;
811 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
812 updateWorkspaceHint(wwin, False, False);
814 if (IS_OMNIPRESENT(wwin))
815 state[i++] = net_wm_state_sticky;
816 if (wwin->flags.shaded)
817 state[i++] = net_wm_state_shaded;
818 if (wwin->flags.maximized & MAX_HORIZONTAL)
819 state[i++] = net_wm_state_maximized_horz;
820 if (wwin->flags.maximized & MAX_VERTICAL)
821 state[i++] = net_wm_state_maximized_vert;
822 if (WFLAGP(wwin, skip_window_list))
823 state[i++] = net_wm_state_skip_taskbar;
824 if (wwin->flags.net_skip_pager)
825 state[i++] = net_wm_state_skip_pager;
826 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
827 state[i++] = net_wm_state_hidden;
828 state[i++] = net_wm_state_skip_pager;
830 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
831 if (!IS_OMNIPRESENT(wwin))
832 updateWorkspaceHint(wwin, True, False);
833 state[i++] = net_wm_state_sticky;
836 if (WFLAGP(wwin, sunken))
837 state[i++] = net_wm_state_below;
838 if (WFLAGP(wwin, floating))
839 state[i++] = net_wm_state_above;
840 if (wwin->flags.fullscreen)
841 state[i++] = net_wm_state_fullscreen;
843 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
844 PropModeReplace, (unsigned char *)state, i);
848 static Bool updateStrut(WWindow *wwin, Bool adding)
850 WScreen *scr;
851 WReservedArea *area;
852 Bool hasState = False;
854 scr = wwin->screen_ptr;
856 if (adding) {
857 Atom type_ret;
858 int fmt_ret;
859 unsigned long nitems_ret, bytes_after_ret;
860 long *data = NULL;
862 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
863 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
864 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
866 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
867 area->area.x1 = data[0];
868 area->area.x2 = data[1];
869 area->area.y1 = data[2];
870 area->area.y2 = data[3];
872 area->window = wwin->client_win;
874 area->next = scr->netdata->strut;
875 scr->netdata->strut = area;
877 XFree(data);
878 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;
929 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
930 } else if (wwin->type == net_wm_window_type_normal) {
933 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
934 layer = WMSunkenLevel;
935 if (wwin->client_flags.floating && WMFloatingLevel > layer)
936 layer = WMFloatingLevel;
938 return layer;
941 static void doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
943 if (state == net_wm_state_sticky) {
944 if (set == _NET_WM_STATE_TOGGLE)
945 set = !IS_OMNIPRESENT(wwin);
947 if (set != wwin->flags.omnipresent)
948 wWindowSetOmnipresent(wwin, set);
950 } else if (state == net_wm_state_shaded) {
951 if (set == _NET_WM_STATE_TOGGLE)
952 set = !wwin->flags.shaded;
954 if (init) {
955 wwin->flags.shaded = set;
956 } else {
957 if (set)
958 wShadeWindow(wwin);
959 else
960 wUnshadeWindow(wwin);
962 } else if (state == net_wm_state_skip_taskbar) {
963 if (set == _NET_WM_STATE_TOGGLE)
964 set = !wwin->client_flags.skip_window_list;
966 wwin->client_flags.skip_window_list = set;
967 } else if (state == net_wm_state_skip_pager) {
968 if (set == _NET_WM_STATE_TOGGLE)
969 set = !wwin->flags.net_skip_pager;
971 wwin->flags.net_skip_pager = set;
972 } else if (state == net_wm_state_maximized_vert) {
973 if (set == _NET_WM_STATE_TOGGLE)
974 set = !(wwin->flags.maximized & MAX_VERTICAL);
976 if (init) {
977 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
978 } else {
979 if (set)
980 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
981 else
982 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
984 } else if (state == net_wm_state_maximized_horz) {
985 if (set == _NET_WM_STATE_TOGGLE)
986 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
988 if (init) {
989 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
990 } else {
991 if (set)
992 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
993 else
994 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
996 } else if (state == net_wm_state_hidden) {
997 if (set == _NET_WM_STATE_TOGGLE)
998 set = !(wwin->flags.miniaturized);
1000 if (init) {
1001 wwin->flags.miniaturized = set;
1002 } else {
1003 if (set)
1004 wIconifyWindow(wwin);
1005 else
1006 wDeiconifyWindow(wwin);
1008 } else if (state == net_wm_state_fullscreen) {
1009 if (set == _NET_WM_STATE_TOGGLE)
1010 set = !(wwin->flags.fullscreen);
1012 if (init) {
1013 wwin->flags.fullscreen = set;
1014 } else {
1015 if (set)
1016 wFullscreenWindow(wwin);
1017 else
1018 wUnfullscreenWindow(wwin);
1020 } else if (state == net_wm_state_above) {
1021 if (set == _NET_WM_STATE_TOGGLE)
1022 set = !(wwin->client_flags.floating);
1024 if (init) {
1025 wwin->client_flags.floating = set;
1026 } else {
1027 wwin->client_flags.floating = set;
1028 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1031 } else if (state == net_wm_state_below) {
1032 if (set == _NET_WM_STATE_TOGGLE)
1033 set = !(wwin->client_flags.sunken);
1035 if (init) {
1036 wwin->client_flags.sunken = set;
1037 } else {
1038 wwin->client_flags.sunken = set;
1039 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1042 } else {
1043 #ifdef DEBUG_WMSPEC
1044 wmessage("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1045 #endif
1049 static void removeIcon(WWindow *wwin)
1051 if (wwin->icon == NULL)
1052 return;
1053 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1054 XUnmapWindow(dpy, wwin->icon->core->window);
1055 RemoveFromStackList(wwin->icon->core);
1056 wIconDestroy(wwin->icon);
1057 wwin->icon = NULL;
1061 static Bool handleWindowType(WWindow *wwin, Atom type, int *layer)
1063 Bool ret = True;
1065 if (type == net_wm_window_type_desktop) {
1066 wwin->client_flags.no_titlebar = 1;
1067 wwin->client_flags.no_resizable = 1;
1068 wwin->client_flags.no_miniaturizable = 1;
1069 wwin->client_flags.no_border = 1;
1070 wwin->client_flags.no_resizebar = 1;
1071 wwin->client_flags.no_shadeable = 1;
1072 wwin->client_flags.no_movable = 1;
1073 wwin->client_flags.omnipresent = 1;
1074 wwin->client_flags.skip_window_list = 1;
1075 wwin->client_flags.skip_switchpanel = 1;
1076 wwin->client_flags.dont_move_off = 1;
1077 wwin->client_flags.no_appicon = 1;
1078 wwin->flags.net_skip_pager = 1;
1079 wwin->frame_x = 0;
1080 wwin->frame_y = 0;
1081 } else if (type == net_wm_window_type_dock) {
1082 wwin->client_flags.no_titlebar = 1;
1083 wwin->client_flags.no_resizable = 1;
1084 wwin->client_flags.no_miniaturizable = 1;
1085 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1086 wwin->client_flags.no_resizebar = 1;
1087 wwin->client_flags.no_shadeable = 1;
1088 wwin->client_flags.no_movable = 1;
1089 wwin->client_flags.omnipresent = 1;
1090 wwin->client_flags.skip_window_list = 1;
1091 wwin->client_flags.skip_switchpanel = 1;
1092 wwin->client_flags.dont_move_off = 1;
1093 wwin->flags.net_skip_pager = 1;
1094 } else if (type == net_wm_window_type_toolbar) {
1095 wwin->client_flags.no_titlebar = 1;
1096 wwin->client_flags.no_resizable = 1;
1097 wwin->client_flags.no_miniaturizable = 1;
1098 wwin->client_flags.no_resizebar = 1;
1099 wwin->client_flags.no_shadeable = 1;
1100 wwin->client_flags.skip_window_list = 1;
1101 wwin->client_flags.skip_switchpanel = 1;
1102 wwin->client_flags.dont_move_off = 1;
1103 wwin->client_flags.no_appicon = 1;
1104 } else if (type == net_wm_window_type_menu) {
1105 wwin->client_flags.no_titlebar = 1;
1106 wwin->client_flags.no_resizable = 1;
1107 wwin->client_flags.no_miniaturizable = 1;
1108 wwin->client_flags.no_resizebar = 1;
1109 wwin->client_flags.no_shadeable = 1;
1110 wwin->client_flags.skip_window_list = 1;
1111 wwin->client_flags.skip_switchpanel = 1;
1112 wwin->client_flags.dont_move_off = 1;
1113 wwin->client_flags.no_appicon = 1;
1114 } else if (type == net_wm_window_type_utility) {
1115 wwin->client_flags.no_appicon = 1;
1116 } else if (type == net_wm_window_type_splash) {
1117 wwin->client_flags.no_titlebar = 1;
1118 wwin->client_flags.no_resizable = 1;
1119 wwin->client_flags.no_miniaturizable = 1;
1120 wwin->client_flags.no_resizebar = 1;
1121 wwin->client_flags.no_shadeable = 1;
1122 wwin->client_flags.no_movable = 1;
1123 wwin->client_flags.skip_window_list = 1;
1124 wwin->client_flags.skip_switchpanel = 1;
1125 wwin->client_flags.dont_move_off = 1;
1126 wwin->client_flags.no_appicon = 1;
1127 wwin->flags.net_skip_pager = 1;
1128 } else if (type == net_wm_window_type_dialog) {
1129 /* These also seem a bad idea in our context -Dan
1130 // wwin->client_flags.skip_window_list = 1;
1131 // wwin->client_flags.no_appicon = 1;
1133 } else if (type == net_wm_window_type_normal) {
1134 } else {
1135 ret = False;
1138 wwin->type = type;
1139 *layer = getWindowLayer(wwin);
1141 return ret;
1144 void wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1146 if (wwin->type == net_wm_window_type_splash) {
1147 WScreen *scr = wwin->screen_ptr;
1148 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1149 *x = rect.pos.x + (rect.size.width - width) / 2;
1150 *y = rect.pos.y + (rect.size.height - height) / 2;
1154 static void updateWindowType(WWindow *wwin)
1156 Atom type_ret;
1157 int fmt_ret, layer;
1158 unsigned long nitems_ret, bytes_after_ret;
1159 long *data = NULL;
1161 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1162 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1163 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1165 int i;
1166 Atom *type = (Atom *) data;
1167 for (i = 0; i < nitems_ret; ++i) {
1168 if (handleWindowType(wwin, type[i], &layer))
1169 break;
1171 XFree(data);
1174 if (wwin->frame != NULL) {
1175 ChangeStackingLevel(wwin->frame->core, layer);
1176 wwin->frame->flags.need_texture_change = 1;
1177 wWindowConfigureBorders(wwin);
1178 wFrameWindowPaint(wwin->frame);
1179 wNETWMUpdateActions(wwin, False);
1183 void wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1185 Atom type_ret;
1186 int fmt_ret, i;
1187 unsigned long nitems_ret, bytes_after_ret;
1188 long *data = NULL;
1190 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1191 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1192 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1194 long desktop = *data;
1195 XFree(data);
1197 if (desktop == -1)
1198 wwin->client_flags.omnipresent = 1;
1199 else
1200 *workspace = desktop;
1203 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1204 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1205 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1207 Atom *state = (Atom *) data;
1208 for (i = 0; i < nitems_ret; ++i)
1209 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1211 XFree(data);
1214 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1215 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1216 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1218 Atom *type = (Atom *) data;
1219 for (i = 0; i < nitems_ret; ++i) {
1220 if (handleWindowType(wwin, type[i], layer))
1221 break;
1223 XFree(data);
1226 wNETWMUpdateActions(wwin, False);
1227 updateStrut(wwin, False);
1228 updateStrut(wwin, True);
1230 wScreenUpdateUsableArea(wwin->screen_ptr);
1233 static Bool updateNetIconInfo(WWindow *wwin)
1235 Atom type_ret;
1236 int fmt_ret;
1237 unsigned long nitems_ret, bytes_after_ret;
1238 long *data = NULL;
1239 Bool hasState = False;
1240 Bool old_state = wwin->flags.net_handle_icon;
1242 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1243 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1244 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1245 long handled = *data;
1246 wwin->flags.net_handle_icon = (handled != 0);
1247 XFree(data);
1248 hasState = True;
1250 } else {
1251 wwin->flags.net_handle_icon = False;
1254 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1255 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1256 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1258 #ifdef NETWM_PROPER
1259 if (wwin->flags.net_handle_icon)
1260 #else
1261 wwin->flags.net_handle_icon = True;
1262 #endif
1264 wwin->icon_x = data[0];
1265 wwin->icon_y = data[1];
1266 wwin->icon_w = data[2];
1267 wwin->icon_h = data[3];
1270 XFree(data);
1271 hasState = True;
1273 } else {
1274 wwin->flags.net_handle_icon = False;
1277 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1278 if (wwin->flags.net_handle_icon) {
1279 removeIcon(wwin);
1280 } else {
1281 wwin->flags.miniaturized = False;
1282 wwin->flags.skip_next_animation = True;
1283 wIconifyWindow(wwin);
1287 return hasState;
1290 void wNETWMCheckInitialClientState(WWindow *wwin)
1292 #ifdef DEBUG_WMSPEC
1293 wmessage("wNETWMCheckInitialClientState");
1294 #endif
1296 wNETWMShowingDesktop(wwin->screen_ptr, False);
1298 updateNetIconInfo(wwin);
1299 updateIconImage(wwin);
1302 static void handleDesktopNames(XClientMessageEvent *event, WScreen *scr)
1304 unsigned long nitems_ret, bytes_after_ret;
1305 char *data, *names[32];
1306 int fmt_ret, i, n;
1307 Atom type_ret;
1309 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1310 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1311 &bytes_after_ret, (unsigned char **)&data) != Success)
1312 return;
1314 if (data == NULL)
1315 return;
1317 if (type_ret != utf8_string || fmt_ret != 8)
1318 return;
1320 n = 0;
1321 names[n] = data;
1322 for (i = 0; i < nitems_ret; i++) {
1323 if (data[i] == 0) {
1324 n++;
1325 names[n] = &data[i];
1326 } else if (*names[n] == 0) {
1327 names[n] = &data[i];
1328 wWorkspaceRename(scr, n, names[n]);
1333 Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
1335 WScreen *scr;
1336 WWindow *wwin;
1337 Bool done = True;
1339 #ifdef DEBUG_WMSPEC
1340 wmessage("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1341 #endif
1343 scr = wScreenForWindow(event->window);
1344 if (scr) {
1345 /* generic client messages */
1346 if (event->message_type == net_current_desktop) {
1347 wWorkspaceChange(scr, event->data.l[0]);
1348 } else if (event->message_type == net_number_of_desktops) {
1349 long value;
1351 value = event->data.l[0];
1352 if (value > scr->workspace_count) {
1353 wWorkspaceMake(scr, value - scr->workspace_count);
1354 } else if (value < scr->workspace_count) {
1355 int i;
1356 Bool rebuild = False;
1358 for (i = scr->workspace_count - 1; i >= value; i--) {
1359 if (!wWorkspaceDelete(scr, i)) {
1360 rebuild = True;
1361 break;
1365 if (rebuild)
1366 updateWorkspaceCount(scr);
1368 } else if (event->message_type == net_showing_desktop) {
1369 wNETWMShowingDesktop(scr, event->data.l[0]);
1370 } else if (event->message_type == net_desktop_names) {
1371 handleDesktopNames(event, scr);
1372 } else {
1373 done = False;
1376 if (done)
1377 return True;
1380 /* window specific client messages */
1382 wwin = wWindowFor(event->window);
1383 if (!wwin)
1384 return False;
1386 if (event->message_type == net_active_window) {
1388 * Satisfy a client's focus request only if
1389 * - request comes from a pager, or
1390 * - it's explicitly allowed in Advanced Options, or
1391 * - giving the client the focus does not cause a change in
1392 * the active workspace (XXX: or the active head if Xinerama)
1394 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace /* No workspace change */
1395 || event->data.l[0] == 2 /* Requested by pager */
1396 || WFLAGP(wwin, focus_across_wksp) /* Explicitly allowed */) {
1397 wNETWMShowingDesktop(scr, False);
1398 wMakeWindowVisible(wwin);
1400 } else if (event->message_type == net_close_window) {
1401 if (!WFLAGP(wwin, no_closable)) {
1402 if (wwin->protocols.DELETE_WINDOW)
1403 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1405 } else if (event->message_type == net_wm_state) {
1406 int maximized = wwin->flags.maximized;
1407 long set = event->data.l[0];
1409 #ifdef DEBUG_WMSPEC
1410 wmessage("net_wm_state set %ld a1 %s a2 %s\n", set,
1411 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1412 #endif
1414 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1415 if (event->data.l[2])
1416 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1418 if (wwin->flags.maximized != maximized) {
1419 if (!wwin->flags.maximized) {
1420 wwin->flags.maximized = maximized;
1421 wUnmaximizeWindow(wwin);
1422 } else {
1423 wMaximizeWindow(wwin, wwin->flags.maximized);
1426 updateStateHint(wwin, False, False);
1427 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1428 updateNetIconInfo(wwin);
1429 } else if (event->message_type == net_wm_desktop) {
1430 long desktop = event->data.l[0];
1431 if (desktop == -1) {
1432 wWindowSetOmnipresent(wwin, True);
1433 } else {
1434 if (IS_OMNIPRESENT(wwin))
1435 wWindowSetOmnipresent(wwin, False);
1436 wWindowChangeWorkspace(wwin, desktop);
1438 } else {
1439 done = False;
1442 return done;
1445 void wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1447 #ifdef DEBUG_WMSPEC
1448 wmessage("clientHintChange type %s\n", XGetAtomName(dpy, event->atom));
1449 #endif
1451 if (event->atom == net_wm_strut) {
1452 updateStrut(wwin, False);
1453 updateStrut(wwin, True);
1454 wScreenUpdateUsableArea(wwin->screen_ptr);
1455 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1456 updateNetIconInfo(wwin);
1457 } else if (event->atom == net_wm_window_type) {
1458 updateWindowType(wwin);
1459 } else if (event->atom == net_wm_name) {
1460 char *name = wNETWMGetWindowName(wwin->client_win);
1461 wWindowUpdateName(wwin, name);
1462 if (name)
1463 wfree(name);
1464 } else if (event->atom == net_wm_icon_name) {
1465 if (wwin->icon) {
1466 char *name = wNETWMGetIconName(wwin->client_win);
1467 wIconChangeTitle(wwin->icon, name);
1469 } else if (event->atom == net_wm_icon) {
1470 updateIconImage(wwin);
1474 int wNETWMGetPidForWindow(Window window)
1476 Atom type_ret;
1477 int fmt_ret;
1478 unsigned long nitems_ret, bytes_after_ret;
1479 long *data = NULL;
1480 int pid;
1482 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1483 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1484 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1485 pid = *data;
1486 XFree(data);
1487 } else {
1488 pid = 0;
1491 return pid;
1494 char *wNETWMGetWindowName(Window window)
1496 char *name;
1497 char *ret;
1498 int size;
1500 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1501 if (name) {
1502 ret = wstrndup(name, size);
1503 XFree(name);
1504 } else {
1505 ret = NULL;
1508 return ret;
1511 char *wNETWMGetIconName(Window window)
1513 char *name;
1514 char *ret;
1515 int size;
1517 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1518 if (name) {
1519 ret = wstrndup(name, size);
1520 XFree(name);
1521 } else {
1522 ret = NULL;
1525 return ret;
1528 static void observer(void *self, WMNotification *notif)
1530 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1531 const char *name = WMGetNotificationName(notif);
1532 void *data = WMGetNotificationClientData(notif);
1533 NetData *ndata = (NetData *) self;
1535 if (strcmp(name, WMNManaged) == 0 && wwin) {
1536 updateClientList(wwin->screen_ptr);
1537 updateClientListStacking(wwin->screen_ptr, NULL);
1538 updateStateHint(wwin, True, False);
1540 updateStrut(wwin, False);
1541 updateStrut(wwin, True);
1542 wScreenUpdateUsableArea(wwin->screen_ptr);
1543 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1544 updateClientList(wwin->screen_ptr);
1545 updateClientListStacking(wwin->screen_ptr, wwin);
1546 updateWorkspaceHint(wwin, False, True);
1547 updateStateHint(wwin, False, True);
1548 wNETWMUpdateActions(wwin, True);
1550 updateStrut(wwin, False);
1551 wScreenUpdateUsableArea(wwin->screen_ptr);
1552 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1553 updateClientListStacking(wwin->screen_ptr, NULL);
1554 updateStateHint(wwin, False, False);
1555 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1556 updateClientListStacking(wwin->screen_ptr, NULL);
1557 updateStateHint(wwin, False, False);
1558 } else if (strcmp(name, WMNChangedFocus) == 0) {
1559 updateFocusHint(ndata->scr, wwin);
1560 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1561 updateWorkspaceHint(wwin, False, False);
1562 updateStateHint(wwin, True, False);
1563 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1564 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1568 static void wsobserver(void *self, WMNotification *notif)
1570 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1571 const char *name = WMGetNotificationName(notif);
1573 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1574 updateWorkspaceCount(scr);
1575 updateWorkspaceNames(scr);
1576 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1577 updateWorkspaceCount(scr);
1578 updateWorkspaceNames(scr);
1579 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1580 updateCurrentWorkspace(scr);
1581 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1582 updateWorkspaceNames(scr);
1586 void wNETFrameExtents(WWindow *wwin)
1588 long extents[4] = { 0, 0, 0, 0 };
1590 /* The extents array describes dimensions which are not
1591 * part of the client window. In our case that means
1592 * widths of the border and heights of the titlebar and resizebar.
1594 * Index 0 = left
1595 * 1 = right
1596 * 2 = top
1597 * 3 = bottom
1599 if (wwin->frame->titlebar)
1600 extents[2] = wwin->frame->titlebar->height;
1601 if (wwin->frame->resizebar)
1602 extents[3] = wwin->frame->resizebar->height;
1603 if (HAS_BORDER(wwin)) {
1604 extents[0] += FRAME_BORDER_WIDTH;
1605 extents[1] += FRAME_BORDER_WIDTH;
1606 extents[2] += FRAME_BORDER_WIDTH;
1607 extents[3] += FRAME_BORDER_WIDTH;
1610 XChangeProperty(dpy, wwin->client_win, net_frame_extents, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) extents, 4);
1613 void wNETCleanupFrameExtents(WWindow *wwin)
1615 XDeleteProperty(dpy, wwin->client_win, net_frame_extents);